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
Now then Ilort, that’s not very helpful. One doesn’t just become a webmaster overnight, it’s a learning process. How would you feel if your teacher said “don’t try to learn until you have learnt”?
Gilank, if your site doesn’t have an .htaccess file just make one in a text editing programme.
Now then, that wasn’t too hard was it.
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
@Pavel
I did as your posted, Opera & Firefox did good but my IE still open the PDF as page not donwload it. Any advice ?
This worked perfectly for me. Thank You!
This worked for me too. The AddType wasn’t working in Chrome on Mac (didn’t test anything other than chrome and safari).
Thanks!
Solution from Pavel Valencia works. (I tried many other advices)
thanks a lot for this tip!
Thanks, this tip works great on Opera & Firefox but not on my IE. I use Win XP SP2 and my IE version is 5.2.
I also tried Pavel Valencia’s tip but it still doesn’t work for my IE.
Any advices will really appreciated.
Note: I’m using WordPress.
Thank you!!
Pavel Valencia’s solution works great. Thanks!
(notice that you need to change the ** with < and > like HTML tags
< should be <
Does anyone know how to make this work for mobile? I’ve tried both Chris’ solution and Pavel’s, however mp3s still stream rather than download on mobile devices. Any ideas?
can you give a example for this how I can link a pdf file to download.
where add the above code
FTP to your site and create a file named .htaccess in your files directory. In that file put the following lines:
Header set Content-Disposition attachment
This particular example will force files with doc, mov, pdf, rtf, or txt to download instead of being shown in the browser.
Cheers
ughh.. looks like this comment area doesn’t render the code.
Just take a look at: http://wiki.dreamhost.com/Htaccess_tricks
Scroll down to the Force File Download section.
it didn’t work for Chrome 17 for Mac…
I came back to post the solution that worked, but it’s almost the same as Pavel Valencia published above
I’m not sure how to mix both, because this one does prevent lower/uppercase issues
Thanks!! Works Nice
Thanks ! This was just what I was looking for and very helpful. Gotta love .htaccess files.
If you’re getting Internal Server error after posting the HTACCESS codes in .HTACCESS document… is there anything amiss? BTW, the website is a WordPress system.
For internal Server error : you must enable header module ->
cd /etc/apache2/mods-available
a2enmod headers
service apache2 restart
Pavel Valencia’s version works like a charm! Thanks
I have created .htaccess file , and applied code as ” AddType application/octet-stream .txt ” , but still the file is opening in browser , it don’t allows to downloading the file.
Hi, I don´t know why but this doesn´t work with Google Chrome. I don´t have a problem with any other browser. Is it only with me? or anybody else has the same problem?
Have you cleared your cache in Chrome? If you have been testing the same file by the same name, you ‘ll need to do that before it will work.
Ok, I’ve been searching ALL DAY for an answer to this, I’m not sure i’m in the right thread but I’m going for it anyway! I’m running on a Windows 7 computer. I just purchased a digital scrapbook paperpack. She says she sent me the 12 individual files in a zipped folder. However, when I downloaded it it appeared in my downloads folder as a blank piece of paper. It appears to be a octet-stream file and none of my programs will open it. I have no idea what to do with it. She then sent me the individual files, unzipped as JPEGS. When I downloaded them the only thing I could open them in is Windows Media Gallery. When I tried to rightclick>copy>paste into a specific folder it appeared again as just a blank piece of paper and am still unable to open it in anything. (Photoshop is where I’m trying to use it) ANY suggestions would be GREATLY appreciated!
this is so very great…. thanks a lot!
Hi – I know this thread has been going for a while and I hate to kick a dead horse, but we are running into trouble with iOS devices.
Has anyone found a solution that works for Mobile Safari? The htaccess adjustment works great for desktop browsers, but iOS devices still stream MP3s.
Any suggestions?
You’d better take this question to StackOverflow and report back with the answers :)
Where would it download to? iOS doesn’t have an accessible file system.
Great trick!
But I have still a problem with video files (.mov, .mp4)
it seems it starts download the file and immediately opens quicktime.
but with large size files quicktime can’t open properly.
Is there a way to force the whole download?
thanks
Chris,
That’s the problem. It doesn’t download. But nekkid MP3 links play on an iOS device. And with several thousand people accessing the several 50MB files for each product our server is under severe strain – maxing our bandwidth @ 1&1. We considered off-loading the files, but the bandwidth costs at Amazon would be more than our revenue…
We need a way to prevent iOS devices from automatically playing nekkid MP3 links. Currently we wrapped ‘em in a ZIP file but that creates a new set of ‘how do I unzip files?’ problems.
Any suggestions?
Thanks. worked for me. I hope future HTML version allow such an option.
I couldn’t get Chromium/Chrome to respond to AddType application/octet-stream .pdf so instead I used htaccess to send the Content-Disposition: attachment header as Chromium Issue 142947 seemed to indicate was preferred. However, this still didn’t work. It seems Chrome interprets Content-Disposition rather strictly, as detailed here by Foo Compels You. Having uppercase characters in my filename caused Chrome to open the file in the in-browser PDF viewer but changing these to lowercase caused the file to download, as intended. Interesting how letter case affects things. Hope this helps.
I would not use this approach as it currently prevents Chrome users from downloading the file.
A link to a PDF it will not do anything when a Chrome user clicks on it.
Chrome 24 – BAD – link does not work at all – user cannot access the file
Firefox 17 – GOOD – works as expected
IE 9 – OK – It downloads the file then opens it with the PDF viewer
I would use the FilesMatch approach one of the other commenters noted – it doesn’t prevent any of your users from getting the file.
<FilesMatch “.(?i:pdf)$”>
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
thx for this tip..
I’ve asked our resident programmer to force our PDF to download, and I received this response, of which, being a graphic designer, not a programmer, I understand pretty much nothing. Is he correct and am I asking for something particularly complex?:
“I know what it takes to force a browser to download rather than
display. In fact I have some pages that do just that. It’s a
server-side issue, because you have to specifically populate the
response header with an “application-octet” stream directive, along with
filename. If you know a special way of doing it client-side, via HTML,
CSS, and/or javascript that all browsers understand in the same way, by
all means let me know and I can put it up. Otherwise it takes hours to
get the server to do it just right. Hours that I won’t have for at
least a few weeks.”
I have been using a series of google drive accounts for download for a 300 meg .exe
as I had no idea on how popular it would be.
I wanted users to stay on the page, so all I did was add “target=”_top”> to the URL.
My download link is a graphic, but the latter does me fine.
Thanks for this tip. I wouldn’t even have thought of using htaccess for this. Glad you were so willing to share!
Hey all,
I made a .htaccess file in the top-most folder (world wide web/sitename/.htaccess) of my site and added the AddType and FilesMatch codes but neither of them seem to work.
I am using Joomla! 3.0 for my companies intranet page and we have a TON of PDF’s that need to be force downloaded. There is no extension that can do this that I know of.
Any other ideas or am I doing something wrong?
Hey guys can someone leave a code?! i really dont understand how to do that :(
Now you can also do this with HTML5.
Source (Click)
Wow! Awesome Marko! I didn’t know that it was possible through the HTML5. Very cool! Thank you for sharing it.
Hi
I have a client who wants to do the opposite. Some browsers open, some download.
Can you override this and force the browser to open in a new window!?
Or is that a really silly question?
Steve