Something interesting happened today. I was working on a project for a client and the requirement was a file upload and downloadable system (with login of course ).. I had completed most of the stuff like login, uploading docs and the right click “save as” worked like a charm. BUT with the latest browsers like chrome and IE9\10, on clicking the link to the document, the files were getting opened either using Google Document Viewer or inside the browser itself – which was a no no for my client..
After trying to pick my brain for couple of hours I figured it could be a server thing and all I had to do was to make sure that I pass the correct header for the respective files (could be .zip, .doc or .pdf or . anything else).
Then it flashed!
I figured it out… The problem was a server problem. Since I was using the WordPress framework to build the site, the header was automatically passed for content and hence the files would open-up inside the browsers. I had to add a small code – just 4 lines in order to make this happen. Here’s what I wrote in order to force the data download:
Header set Content-Disposition attachment
And that was it! All those hours of brain picking finally paid off
I added the above code to the .htaccess file and the rest was taken care by my server Let me explain the above code:
- FilesMatch: Simply tells the apache server for the document types. Here I only need to make .doc, .pdf, .zip etc as downloadable.
- ForceType application/octet-stream: This told apache that the file types listed above is a generic content (MIME) type. So the browser should try to will always show the “open with” or “save as” download dialog if a server sends a file with such a MIME type.
- Header set Content-Disposition attachment: Then I set the header of the webpage manually and woah! We’re done and that’s it!
For anyone having similar trouble.. Hope this helps!