You are here:Home»KB»Web Design»General»Force Files to Download (Not Open in Browser)
Monday, 01 June 2015 17:31

Force Files to Download (Not Open in Browser)

Written by

There might come times when you click on a link to a file, for instance a PDF,  and it opens in the browser but instead you want it to download.

If we take PDFs as an example, these will always open in the browser because that is how Adobe Reader is rigged to show the PDF, in the browser. To do otherwise we need to force the download.

Well there are solutions and i will outline them below.

HTML Download Tag

There is a new config option that you can use in a hyperlink

<a href="/path/to/file" download>Click here to download</a>

The code above does work but if you add this into a WYSIWYG it is slightly modified, and that new version also works so is probably a better version.

<a href="/path/to/file" rel="external" download="">Click here to download</a>

Notice that the download option now has parenthesis to hold a variable so it looks like a normal option. The rel="external" seems to be optional and could just be added by the JCE editor as a matter of course.

<a href="/path/to/file" download="">Click here to download</a>

Variant

This could be the same command used better and also allows you change file names which is useful for dynamically create content.

<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense Report</a>

Old HTML Hack

This is a bit of a hack and is listed for completeness. It will cause a windows to open which will trigger the download and hopefully the new window will also dissapear.

<a href="/path/to/file" download target="_blank">Click here to download</a>

PHP Headers

You can alter the Content-Type headers via your PHP session

application/force-download

By default all files are delivered using the content type application/force-download, which forces all browsers to display a “save file as” dialog instead of displaying the file.

.htaccess

This is a useful method because you can globally change download behaviour using the htaccess. The method is similiar to the PHP method where you change the headers sent to the browser but it is done on the server level.

Specify Files or File Types

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>
  • This will cause all PDFs to download.
  • You can change the file type that the rule affects or add more.
  • With FilesMatch you can target specific files.

Global

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf
  • Above i have included a few version of this declaration you can put in the .htaccess file.
  • You only need to add the rules that apply to the files you want to force to download.
  • These command are global.

Other

These are other methods i have come across that might be useful to other people.

1. I found a very simple solution for firefox (only works with relative rather than direct href): add type="application/octet-stream"

<a href="/./file.pdf" id='example' type="application/octet-stream">Example</a>

2. Disable PDF plugins, this is more for the end user and is not applied on the server.

Tools> Add-ons> Adobe Acrobat(Adobe pdf plugin for Firefox)> DISABLE

Links

Read 5674 times Last modified on Tuesday, 14 February 2017 13:07