Force a File to Download (PDF, Image, Audio) Rather than Showing up in the Browser


forcing-an-item-to-download-graphic

If you run a website where you offer downloadable content to your visitors this tutorial will help you ensure that the item is always saved to their computer. Without putting steps in place to force a file to download, some browsers will only display the item rather than download it.

While simply showing the downloadable item such as an image and allowing the customer to download it at their pleasure is fine for some websites, other times it makes sense for the item to be directly downloaded to the visitors computer.

In this tutorial we will outline how you can always force a file to download rather than showing it in the visitors browser.

When to Force a Download and Why

If you are offering a large item, even if this is a pdf it is recommended to force this download. Your visitor may want to read this later on so having it saved to their downloads file can allow them to do this.

If your customer has paid money to purchase the download, then forcing the download can ensure that they receive what they have paid for. While viewing the download within the browser can often work for images, your user may forget to save it prior to exiting. This means that they no longer have a copy of what they purchased.

How to Force a File Download

If you wish for all your downloadable content to be automatically saved on your visitors computer complete one of the following options (depending on your developer knowledge and website needs):

Option 1 to Ensure Items are Forced to Download: Zip File

Zip File Example

Encapsulating your downloadable items in a zip file will ensure they are forced to download. This is a simple technique that forces the download as browsers are unable to read a zip file.

The majority of your users will know that they will need to open this file to then receive their downloadable content.

Steps to Placing my Download in a Zip File

  1. Save your downloadable item to your computers desktop.
  2. On your computers desktop, right click on the item.
  3. Choose the ‘Send to‘ option and then choose ‘Compressed (zip) folder‘.
  4.  This will place your download in a zip folder.
  5. When attaching your downloadable item, choose the one that has been placed in the zip folder.

placing-download-in-zip-file

Option 2 to Ensure Items are Forced to Download: Use of Code

Adding some code to your .htaccess file of your server can ensure you force particular downloads.

The following example demonstrates the code that you would need to add to force PDF files to download:

<FilesMatch ".(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

You can change the word “pdf” to any other file type based on your need. Some file examples include: mp3, mp4 and doc.

The example below displays how you can force the download of multiple different files including: mov, mp3, jpg and pdf.

<FilesMatch ".(mov|mp3|jpg|pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

Option 3 to Ensure Items are Forced to Download: When Using Amazon s3

If you are storing your files to your Amazon S3 account (instead of your server) then you can complete the following to ensure a force download of those files takes place.

Set the correct headers on your files in Amazon S3 in order to force the browser to download rather than opening the file. You will need to set them when uploading the files to S3.

Set the following:
Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream

Option 4 to Ensure Items are Forced to Download: When Using DropBox

If you have your downloadable item stored in a drop-box account, there is a way to ensure this item is always forced to download when a user clicks on it.

If you want the contents of your drop-box link to download rather than to be displayed in the browser, you can add the parameter “dl=1” to the end of the download link. See the example below for the correct positioning of this parameter:

https://www.dropbox.com/s/a1b2c3d4ef5gh6/example.docx?dl=1





Source link

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)

RSS
Follow by Email
Facebook
Facebook
Twitter
Instagram