Code Snippet
Force Files to Download (Not Open in Browser)
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
make sure to put this poop in .htaccess
Wow, I’ve been trying to figure out how to do this for a very, very (very) long time! Is there any way you can make it specific, for example, if I click “link 1″, I can view the file in browser, but if I click “link 2″, the “AddType” function is applied, and I am forced to download?
Thanks in advance, and sweet snippet!
It’s not an ideal solution to your problem, but you could have a php wrapper for download links that simply takes an argument that is the filename and then reads the file (assuming local files) and before outputting the file to the user you send headers with the filetype. see http://www.php.net/manual/en/function.header.php for more information about php and headers.
This is a great one. I had forgotten about it before but I’m glad I found it again!
Thanks
What abouton a windows server? I can’t get the .htaccess for to work – is this only for apache users?
Correct. Anything .htaccess is only Apache.
Hi!, that was awesome, i just found this tutor on google,
thanks!, and now its possible to not use header function.
I m using apache2triad and in this there is no .htaccess named file so now what todo?
now stop trying to be a webmaster until you know what you’re doing
I like it short ;)
AddType application/octet-stream .doc .mov .avi .pdf .xls .mp4
thanks a lot!!!
Wow, beautiful trick! Was trying to figure this thing out forever, thank you!
Awesome!
Thank youy so much for that trick.
great post /…… :)
Hi,
Some people use:
ForceType application/octet-stream
Header set Content-Disposition attachment
Do you know wich’s better?
Thanks!
I have used AddType application/octet-stream .png and it forces the download, the only issue is that it opens a new empty tab that it closes after it finishes the donwload, but if I hit the cancel button then the new empty tab stays open. Is there anyway to get the pop up window in the same tab that I click the link
don’t use target=”_blank” on you link?
thank u very much i have been trying abouth 2 hours :)
does this work for you in all browsers? i’ve got it only working in chrome but not ie and firefox
ah it’s like that per default in chrome, so i’m not sure what i made wrong, nothing but that in the .htaccess?
Just perfect :-)
Hi!
I test this trick, and work perfect in Chrome 15, Firefox 7, but in IE 9 this broken… show many invalid characters.
Searching more info found this code:
Please replace ** by as appropriate, for some reason in the comments do not see these characters.
Test again and voila work in Chrome, Firefox and IE. In FF and IE show “save as…” dialog box, but in chrome download directly. How to fix this last issue? Force to Chrome show “save as…” dialog box…
Greetings from Peru
Pavel
Solution from Pavel Valencia works. (I tried many other advices)
thanks a lot for this tip!
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.