Downloading a file on clicking on a link

HTML <a> download Attribute is used to download a file when clicking on the link (instead of navigating to the file).

Example:

<a href=”./completefile.pdf” download class=”download-btn”>Download eBook </a>

The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink.

Can be used with or without a filename value:

  • Without a value, the browser will suggest a filename/extension, generated from various sources: – The Content-Disposition HTTP header , The final segment in the URL path , The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)
  • filename: defining a value suggests it as the filename. / and \ characters are converted to underscores (_). Filesystems may forbid other characters in filenames, so browsers will adjust the suggested name if necessary.

Leave a comment

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