Tips and Tricks HQ Support Portal › Forums › General Stuff › Instructions/F.A.Q › Force a File to Download Instead of Showing Up in the Browser
- This topic has 12 replies, 7 voices, and was last updated 9 years, 4 months ago by HowardYeomans.
-
AuthorPosts
-
January 14, 2012 at 5:44 am #5280adminKeymaster
How Browsers Work With File Downloads
Usually when a user goes to a file URL (for example: a download link), the file will show in the browser if the browser supports it. Image files like jpg, png, gif etc. will almost always show in the browser. Archive files like zip, tar, gzip etc. will always be downloaded. Some file types show up in some browsers but not others depending on if the browser can read the file or not. For example, Internet Explorer (IE) will usually try to show Microsoft Word files (doc and docx) in the browser, while most other browsers will download it. Google Chrome has its own PDF converter and it will try to convert a PDF file and render it in the browser.
The key thing to understand is that some browsers maybe able to read a particular file type based on the addons you have installed for that browser while others may not be able to. If a browser can read the file type it will show it in the browser. If the browser cannot read a file type it will force a download to the hard disk. Usually this is not an issue since the users can save the file to their computer after it is shown in the browser.
How to Force a File Download For All My Files
Some users prefer that all the files should be forced downloaded (no showing in the browser). There are a few things you can do to ensure that.
Option 1: The easiest solution is to put your file (example: a PDF file, a movie file) inside a zip file if possible (If you are using the PDF Stamper plugin then you can’t do this unfortunately). Browsers cannot read zip file so it will force a download. Most computer users know to unzip the file and get the content from inside.
Option 2: Most browsers have a settings where it lets the users specify if they want to force download a certain type of files (for example: a pdf file). You can instruct your users to use this option if you think it will help them (again this is common knowledge and most internet users know this).
Option 3: You can add the following lines of code in your .htaccess file of your server to force a download of a particular file type from your server (in this example it is for PDF file types):
<FilesMatch ".(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
You can change “pdf” to any other file type based on your need (example: mp3, mp4, doc).
Force File to Download from Your Amazon S3 Account
If you are storing your files to your amazon S3 account (instead of your server) then do the following to force a download on those files.
Set the correct headers on your files in Amazon S3 in order to force the browser to download rather than opening the file. Set these:
Content-Disposition: attachment; filename=FILENAME.EXT Content-Type: application/octet-stream
You will need to set them when uploading the files to S3. You can also edit an existing S3 object and adjust the Metadata values by going to the properties tab of the object in question.
The following is an example of what values to enter in the Metadata section of an S3 object to force a download.
March 31, 2012 at 5:52 pm #40742bobkSpectatorIs there any way to accomplish option 3 with files hosted on Amazon S3?
I’m setting up a site to sell audio files.
The sample excerpts are stored at my web site, and I DO want those to play in the browser (via the plugin mp3-jplayer for WordPress).
The full size audio files for purchase will be stored at S3.
I realize option 1 is the easiest (and I’ve heard that zipping also helps prevent file corruption in transit), but it adds an extra step for the customer. Probably not a big deal, but I’d like to know what my options are. Much of the S3 documentation is above my skill level, but I’ll check there as well.
Thanks!
March 31, 2012 at 10:14 pm #40743wzpModeratorUnverified, but I’ve heard that you can use the Amazon S3 Console to change the content type & disposition on a per object basis.
March 31, 2012 at 11:54 pm #40744bobkSpectatorYes – you’re exactly right, wzp! Thank you! I figured out what to add based on option 3 above.
For other newbies who have this issue, here’s the workflow:
In the S3 console, select an object (in my case, an audio file), and then click the ‘properties’ button (if the properties window at the bottom isn’t already visible).
Click the ‘metadata’ tab.
In my case, ‘Key’ was by default set to ‘Content-Type’ and ‘Value’ was set to ‘application/octet-stream.’ I left that as-is. (It was an AAC (.m4A) file; for an .mp3 file, the Content-Type was set to audio/mpeg. I left that unchanged too.)
Click the ‘add more metadata’ button, and add:
Key: Content-Disposition Value: attachment
I then ran a test purchase in the PayPal Sandbox, and the files downloaded instead of loading in the browser, which they had previously done. Cool!
Setting each object manually might be a pain for those with large numbers of files, but it’ll be fine for me.
Follow up question:
Previously, when the audio file loaded in the browser (instead of downloading), the URL in the address bar included my S3 Access Key ID – not simply the encrypted link.
Is that a security risk?
Thanks again.
April 1, 2012 at 1:00 pm #40745wzpModeratorNo, that is not a security risk. S3 uses a public/secret key system. You are only seeing the public key. The secret key is used to encrypt the expiration information. S3 uses the public key in the URL, and the secret key that it knows is associated with the public key, to recreate the encrypted expiration part of the URL.
April 1, 2012 at 9:07 pm #40746bobkSpectatorGreat! Thanks very much.
Bob
April 16, 2012 at 3:40 am #40747subfightMemberThanks bobk for the details about option 3, it works great for me too ))
April 16, 2012 at 4:25 am #40748subfightMemberI have found a way to add the metadata to all the files of a bucket or just a folder in a couple of clicks with CloudBerry Explorer.
1- right click your folder (or bucket) and select “Set Http Headers”
2- In the new window, click “Add”
3- For Http Headers, choose: “content disposition” and for Value: attachment; filename=
4- Click “OK”
5- At the bottom, select: Keep existing headers, overwrite values for headers with the same name and add new headers (from the list specified above)
6- Then, all your files will be modified in seconds.
September 7, 2013 at 5:20 am #40749ellie123MemberHello –
Is it possible to force a download to mobile devices? For instance can we prompt the user to auto install an .apk file for android devices? Or an html file for web capable apps? Pls advise on procedure if so. My files are also stored on S3 and each product has its own post and protected download button.
Thank you
September 7, 2013 at 11:45 am #40750wzpModeratorYou cannot “auto install” an app to a mobile device. Modern mobile devices have security features that prevent this; unless the owner takes specific actions to allow it; which are comparable to asking them to program their VCR clock. You can however, use the APR feature of eStore to permit access to protected pages and web apps.
http://www.tipsandtricks-hq.com/ecommerce/using-wordpress-permalinks-as-digital-products-apr-1217
March 16, 2015 at 10:17 am #40751VictorSinclairMemberI tested your code from option 3 in my htaccess file as you suggested above. I have the setting of my test mp3 set to UNdownloadable, in Digital Content Details. I found that if the mp3 is set to downloadable, it doesn’t work on mobile ios devices. Since this being to undownloadable works for mobile ios, I was trying to use the above code to force a download on PC, but it has no effect. The mp3 still immediately plays in the browser? Is the code correct?
March 16, 2015 at 10:54 pm #40752adminKeymasterDid you change the file type from “pdf” to “mp3” in the code? What exact code did you use?
July 7, 2015 at 9:38 pm #40753HowardYeomansMember‘Thumbs up’ option 3. Thank you.
-
AuthorPosts
- You must be logged in to reply to this topic.