Forcing a File to Automatically Download

A site visitor's browser will always attempt to display any content it can inline. This behavior covers simple things, like text or images, as well as content which requires a specific plug-in to render, such as Flash or Acrobat (PDF) files. If the browser "believes" it can render the content, it will try to do so inline.

This may be at odds with your design intent. For example, you may want your product spec sheets to always download to the user's computer, so that they will be saved and reviewed later. If the browser is configured to show this content inline, it will instead be downloaded to the browser's cache, and the user will either need to know the specific sequence of menu commands and options to select in order to save the file somewhere "memorable", or will need to be instructed to Command/Control-click on the link in order to force a download.

Most Web servers support the .htaccess file as a simple means of adding extended commands to the server configuration. Note however that this is entirely up to the owner of the server to allow these modifications. If you are not able to effect the desired change, you will have to work with the tech support staff of your ISP (or in extreme cases, seek a new hosting provider) to get the results you need.

The following instructions are specific to Apache web servers, or servers that deliberately copy the configuration directives made popular by Apache. The vast majority of the world's commercial Web hosting providers use Apache, so it's a fair bet that this will work without asking, but if it doesn't, "what server software are you using?" would be a good first question to ask.

Create a text file, using a tool like TextEdit (in plain-text mode) or BBEdit or TextWrangler which can save a truly un-styled plain text file. Do not use Word or another "rich text" editor for this, or you will just cause heartache for yourself.

Type the following lines into the file:

<
FilesMatch "*.pdf">

    ForceType application/octet-stream
</FilesMatch>

Replace .pdf with whatever file type you want to force to download (leave the * as it is though). Save the file, calling it whatever you like.

Upload the file to your server using an FTP client such as Transmit or Cyberduck, placing the file inside the same folder as the rest of your site files.

Once the file is in your "site root" folder, click on its name once to highlight it in the list, then a second time to enable editing of the filename. It is important not to double-click on the file, as this will download it or open it for editing in your text editor, depending on how your preferences are set.

Change the name of the file to .htaccess (with a leading dot).

This may cause the file to disappear from the list. If it does, use the View menu (in Transmit) to enable "Show invisible files". Other FTP clients have similar settings possible, often in the general Preferences dialog.

That's all there is to it. If your Web server allows these sorts of configuration changes, the very next visitor to your site will be forced to download all files with the extension .pdf. They will not display in the plug-in.


Related Articles

Attachments

No attachments were found.

Article Details

Last Updated
14th of August, 2008

Would you like to...

Print this page  Print this page

Email this page  Email this page

Post a comment  Post a comment

 Subscribe me

Subscribe me  Add to favorites

Remove Highlighting Remove Highlighting

Edit this Article

Quick Edit

Export to PDF

Share |


User Opinions

No users have voted.

How would you rate this answer?




Thank you for rating this answer.

Continue