The HTML 5 anchor tag: download attribute

The humble anchor tag. It’s the lynchpin of the web, allowing links between pages. Surely there’s nothing to improve here?

Well, one problem I encountered today is how to put a link to a resource (mp3, pdf or similar) and make sure that when the link is clicked it isn’t rendered in the browser but instead is downloaded. It turns out that HTML5 allows us to do just that, by adding the download attribute to the link. If the attribute is set to a value, that will be the filename of the download.

To force a download:

<a href="myfile.pdf" download>Download your file</a>

To set a filename:

<a href="myfile.pdf" download="mydownloadedfilename.pdf>Download your file</a>

Leave a Reply

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