Force browser to view pdf instead of download

Force browser to view pdf instead of download

force browser to view pdf instead of download

You will have to teach your Chrome how to Preview PDF's. For that you need to install a PDF preview "add on" in Chrome. Automatically previews pdfs. -- The ideal is that when users cick the url in the email, the PDF opens in a new web-browser window (and doesn't download to their desktop). www.cronistalascolonias.com.ar › questions › how-do-i-force-files-to-open-in-the-brow. force browser to view pdf instead of download

Question: Force browser to view pdf instead of download

Force browser to view pdf instead of download
Force browser to view pdf instead of download
Force browser to view pdf instead of download
Force browser to view pdf instead of download
Force browser to view pdf instead of download

Force Download PDF (Instead of Browser View)

There are situations where a PDF (or other file) needs to be downloaded to the site visitor's computer. Not just loaded into the browser.

These might be sales tools, how-to manuals, price lists, any document to be shared independently or referred to repeatedly.

Loading a PDF into the browser generally also provides a way to save the document to the person's computer. But it's an extra step. Some simply won't do that extra step (or don't know how). The site visitor could also right-click to download the file, but not all will do that or know it can be done.

If it needs to be downloaded, be friendly and make it easy to download with a click or a tap. (A tap for mobile devices that can download.)

Further below is a short PHP script to help you implement exactly that. The script initiates a file download instead of sending the file to the browser window.

It's similar to how Download Handler works, except Download Handler (among other things) counts downloads and can keep file locations secret.

The PHP script further below is simple download-one-file software without the other features Download Handler has.

If it needs to be downloaded, be friendly and make it easy to do with a click or a tap.

Tweet this

How It Works

The Download-a-File software further below is a PHP script you'll upload to your server.

  1. In your web page, link to the download script. The URL of the download script is followed with a "?" and the location of the file to download.

  2. When the link is clicked, the download script causes the file to be downloaded.

That's it. Here's an example link:

<a href="www.cronistalascolonias.com.ar?/download/www.cronistalascolonias.com.ar"> Download the "something" document </a>

The Download-a-File Software

Here's the Download-a-File source code. No modifications necessary. Simply name it www.cronistalascolonias.com.ar and upload it to your server.

<?php /* Download-a-File Version March 30, Version - original beta version - March 29, Version - enforce leading "/" in file location, bug fix, code tweaks - March 30, Will Bontrager Software LLC www.cronistalascolonias.com.ar Copyright Will Bontrager Software LLC This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose. Will Bontrager Software LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. */ if( empty($_SERVER['QUERY_STRING']) ) { ExitWithMessage('Inappropriate access.'); } $location = $downloadfile = trim(urldecode($_SERVER['QUERY_STRING'])); if( strpos($downloadfile,'/') !== 0 ) { $downloadfile = "/$downloadfile"; } $downloadfile = "${_SERVER['DOCUMENT_ROOT']}$downloadfile"; if( ! file_exists($downloadfile) ) { ExitWithMessage("Unable to find file $location"); } if( ! ($filesize = filesize($downloadfile)) ) { ExitWithMessage("File $location seems to be empty."); } $ta = explode('/',$downloadfile); $downloadfilename = array_pop($ta); header("Pragma: private"); header("Expires: 0"); header("Cache-Control: private, must-revalidate"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$downloadfilename."\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: $filesize"); readfile($downloadfile); exit; function ExitWithMessage($s) { echo $s; exit; } ?>

The file may be named something other than www.cronistalascolonias.com.ar so long as it has a .php file name extension.

Use the Download-a-File software by linking to it. Append a "?" character to the software URL followed by the location of the file to download. The location must be the URI relative to the document root. As an example, if the file's URL is www.cronistalascolonias.com.ar then the file's location is /place/www.cronistalascolonias.com.ar

The software will respond with an error message if any of these conditions exist:

  • No file was specified.
  • The specified file doesn't exist on the server.
  • The specified file is empty.

Otherwise, the software will cause the file to be downloaded. The browser remains on the page with the link.

With few exceptions, any file can be downloaded this way. Exceptions would include when the browser's preferences forbid it or the browser is on a system with no established location for storing downloaded files.

On your web pages, link to www.cronistalascolonias.com.ar with the location of the file to download. Kinda like magic, the file downloads when the site visitor clicks the link or, for mobile devices that can download, taps the link.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Источник: www.cronistalascolonias.com.ar

Force browser to view pdf instead of download - remarkable, very

Force browser to view pdf instead of download

2 thoughts to “Force browser to view pdf instead of download”

Leave a Reply

Your email address will not be published. Required fields are marked *