How to force a file to download (Not open in a browser) Print


Modern browsers have very good file handling capabilities but there are times when you would prefer to force a file to download rather than have it opened in the browser. To achieve this you need you need to override what the browser is trying to do and let your operating system handle the file. This is easy to do with a simple code within your .htaccess file.

AddType application/octet-stream .[filetype]

Example

For example, as standard the browser is able to handle text files and it would take over the page that you are currently viewing, but by by inserting the code below when the link is clicked you will receive a window from your operating system asking where to save the file rather than showing the file in the browser window.

<IfModule mod_mime.c>
AddType application/octet-stream .txt
</IfModule>

Which files would you want handled by your operating system?

The example of files that you would like to download are:

  • Microsoft Office Documents: Word, Excel, PowerPoint [.doc,.docx.xls,.xlsx,.ppt]
  • Comma separated Values [.csv]
  • Text Documents [.txt]
  • HTML file as a download [.html,.htm]
  • Portable Document Format [.pdf]
  • Movie files to download rather than play [.mpg,.avi,.mov]

How to force a Web Page to Download

Your browser is designed to handle web pages; In fact this is it's primary function! So what do you do when you want to offer a web page as a download?
One of the beautiful things with .htaccess files is that it will override the standard server configuration for the folder AND any subfolders that it is placed. This means that if you specify for a filetype to download in your /public_html/ folder this will affect your whole site, so to get around the issue that you want your site to display normal html webpages, the download must be put into a separate subfolder. Within your cPanel web hosting package create a folder called /public_html/downloads/ and in the .htaccess file within that folder add this code:

<IfModule mod_mime.c>
AddType application/octet-stream .html
AddType application/octet-stream .htm
</IfModule>

I hope this helps and if you have any question please let me know if the comments below


Was this answer helpful?

« Back
Venatour Sports Travel

Encode have continually surpassed themselves in developing our new website. Their patience in learning and understanding what we needed from a website to provide the best service to our customers has been beneficial on so many levels. I would highly recommend them to anyone looking for intelligent solutions to their web, seo or business needs.

Lisa Cooley

Positive: Professionalism, Quality, Responsiveness, Value

Working with Neil is a dream; he's fast, efficient, responsive and full of website knowledge and advice. He won't baffle you with IT over load and will be honest about the best working solution.

Highly recommended!

Ellie Stevenson Author

Positive: Professionalism, Value

Encode and I had a very useful discussion about Wordpress and related issues at no cost to myself. He gave me some very helpful information and answered the questions I had.

I learnt quite a lot! Thank you.

Silvia Boev

Positive: Professionalism, Quality, Responsiveness, Value

I am very excited to work again with Neil. His attention to detail is second to none. I have seen his work in action in the past and he is great. I love his calm approach and his patient guidance to non-tech people like me. I would highly highly reccommend him!

Dr Fuz

Neil is the best! He has sorted out all our business emails and everything runs a great deal better now. Website issues are now a thing of the past as Neil has introduced a maintenance programme to ensure the site is always working optimally and any content changes are actioned pretty much immediately.

Highly recommended!!

H Scott-Brown

'Encode' provided my hosting and domain transfers. They were able to deal with the technicalities of the transfer in a particularly speedy manner, taking all the worry of the transfer, domain issues DNS and nameserver headaches as well as the migration. A couple of emails, and phone call meant it was done smoothly, swiftly and efficiently. All I can say is I'm really please they were recommended to me. Thank you

Spinner