How to Create an HTML Download Link?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Downloading files refers to receiving information from a server so that later on it can be viewed offline. Millions of downloads take place every day. A download link on a website is used to download a specific file when the user clicks on it. The anchor <a> tag along with its href and download attributes assist in implementing this feature. The file to be downloaded can be a pdf, zip, jpg, png, doc, etc.

Syntax

Download Restrictions

The download attribute works for same-origin URLs, that is only files that belong to that website can be downloaded. In simple terms, if href does not have the same origin as the site, the file won't be downloaded.

What is the Same-Origin Policy?

The same-origin policy is a security mechanism that removes documents that might cause cyber threats. The policy helps users to download only those files that are from the origin site.

same-origin policy

HTML download link is created using the <a> tag. The value of the href property is set to the location of the file to be downloaded, and a download attribute is added. The new filename of the downloaded file can be set as the value for the download attribute. The original filename is used if no value is specified.

Code

original file name links with black bg

The property supports all file extensions, and the browser detects it (.img, .pdf, .txt, .html, etc.).

Users can right-click on the link and choose 'Save As' from the list of options to download the file to their local system.

Note: Before creating the download link, ensure that the file is uploaded to the specified location.

ZIP File Download Link

MP3 File Download Link

PDF File Download Link

Browser Support

HTML download link is supported by the following browsers.

  • Google Chrome
  • Firefox
  • Microsoft Edge
  • Safari
  • Opera

Learn more

Conclusion

  • A download link helps to download a file from the server to the browser's directory on the local disk.
  • The download attribute is used after the href attribute, to make the link downloadable.
  • If a value is assigned to the download attribute, it becomes the new filename of the downloaded file. The original filename is used if no value is specified.
  • There are no restrictions on the file extension type of the file to be downloaded (.img, .pdf, .txt, .html, etc.).
  • The download attribute ensures security by permitting only same-origin URLs, that is only files that belong to that website can be downloaded.