Offer files for download in HTML5: a[download]

[2012-04-16] dev, html5, webdev
(Ad, please don’t block)
The new attribute download for <a> tags allows one to offer a file for download – instead of displaying it in the browser.

Previously. Normally, when you click on an <a> tag, the web browser visits the file it refers to. Sometimes, you instead want the file to be downloaded. Most browsers usually ask the user to confirm before starting the download. Previously, you had to send the following HTTP header to make that happen.

    Content-Disposition: attachment; filename=my-icon.png

Now. The new attribute download ensures that a file is downloaded and not displayed.

    <a href="http://example.com/3zw1456.png"
       download="my-icon.png"
    >download me</a>
Giving the attribute a value allows you to control the name of the file that is saved to disk.

Use case. This feature is especially useful if you are working with URLs coming from Blob instances (which include File instances in the user’s file system) and FileEntry instances (in browser-local sandboxed file systems). With download, their data becomes downloadable.

Source: “Downloading resources in HTML5: a[download]” by Eric Bidelman for HTML5 Rocks Updates.