Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 246 posts at DZone. You can read more from them at their website. View Full User Profile

Offer files for download in HTML5

04.26.2012
| 2408 views |
  • submit to reddit
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.

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)