Force File download – Do Not Display Open In Browser

by Hiroshi on October 27, 2008

in Web Guidelines

In your HTML directly link to the file which is to be downloaded.
i.e.

<a href="/files/somefile.pdf">Download Pdf</a>.

You will get a pop-up box asking whether you want to save the file or open it directly. Now you want to download it. The solution is to open your .htaccess file and write there few lines according to your requirement mentioning extension of files which you want to download directly and you are done.

Place the following code into your .htaccess file.

AddType application/octet-stream .pdf
AddType application/octet-stream .doc
AddType application/octet-stream .csv

This script will force download all files having extensions .pdf, .doc or .csv.

.htaccess File

.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

There is .htaccess file present by default at the root directory of your server. If not, you can always create a file named as .htaccess, code it and upload it in the desired directory or at the root level of your website.

Related Posts

Previous post:

Next post: