Thursday, July 25, 2019

Trigger to Download a File When Clicking Link

There are two ways to trigger to download a file.
  1. HTML 5 Download Attribute.
  2. Download File Using PHP Script.
The download attribute triggers a force download but it does not supported by Safari. Following are the versions that support download attribute.
Chrome = 14.0
Internet Explorer = 13.0
Firefox = 20.0
Opera = 15.0

Download File Using Using HTML 5 Download Attribute

If you want to rename the file at the time of downloading, use the following code:

Download File Using PHP – Server Side Script

We will need to create PHP script file and pass the file name in the href attribute that we want to download as you can see below:

PHP Script

The above file will read the file name and trigger to force download. This example will work on all browsers.
Note: I used this example for PDF file type, you can change it as per your requirement.
If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

No comments:

Post a Comment