Force File download – Do Not Display Open In Browser

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

<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

1 thought on “Force File download – Do Not Display Open In Browser

  1. Phil Reply

    Thanks a lot for the tip, now my visitors don´t get stucked when open a corrupted pdf than collapsed their browsers. :D

Leave a Reply

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