Click here to Skip to main content
Click here to Skip to main content

HTML5 Download Attribute

, 8 Aug 2013
Rate this:
Please Sign up or sign in to vote.
How to use download attribute (HTML5 feature)

Introduction

HTML5 came with new APIs, new input types, as well as new attributes for Forms. One of the upgrades that came with HTML5 is the Download attribute. Now the question arises:

Let's Start

As we know, there are many files that are not downloaded directly. For example: images, webpages, PDF files, music files, etc. We have to right click on images and then click on Save Image to save an image file. But if I want to download an image file directly, then I have to use the download attribute.

Using the Download Attribute

<a href="Image.jpeg" download>Download image</a>

Simply type download in the <a> anchor tag. By using this, when we click on download image, the image starts downloading. The same method is used for downloading PDF, webpage files (HTML), etc. (I am attaching a file in which I have shown a comparison between using the download attribute and not using it.)

If we want to give a new name to the download file, then we have to write:

<a href="Folder/myimagehavenoname.png" download="myImage">Download image</a> 

Here, we write download="myImage". When we click on Download image, then an image file is downloaded with  name myImage with a .png extension. The browser automatically detects the correct file extension and adds it to the downloaded file. So there is no need to add the file extension in the download attribute.

Check Browser Support

For checking our browser support download attribute, we have to use JavaScript:

var a=document.createElement('a');
if(typeof a.download !="undefined")
{
    document.write('Download attribute supported');
}
else
{
    document.write('Download attribute not supported');
}

Currently, Chrome 14+ and Firefox 20+ supports the download attribute (tried and tested by me on Chrome).

I am including the .zip file for downloading in which I am showing the use of the download attribute as well as checking browser support for the download attribute.

That's all. Hope you like it. Thanks!

My Other Posts (Tips and Tricks)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anoop Kr Sharma
Student
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 PinmemberIslamHossam18-Aug-13 1:51 
GeneralRe: My vote of 4 PinprofessionalAnoop Kr Sharma18-Aug-13 6:51 
GeneralMy vote of 2 PinmemberNicolaj Nyvang13-Aug-13 1:32 
GeneralRe: My vote of 2 PinprofessionalAnoop Kr Sharma13-Aug-13 8:00 
GeneralMy vote of 5 PinmemberKarthik. A8-Aug-13 17:35 
GeneralRe: My vote of 5 PinprofessionalAnoop Kr Sharma8-Aug-13 18:29 
GeneralMy vote of 5 PinmemberAnurag Gandhi28-Jul-13 5:09 
GeneralRe: My vote of 5 PinprofessionalAnoop Kr Sharma28-Jul-13 7:48 
GeneralMy vote of 4 PinmemberSsntosh Chouhan27-Jul-13 8:26 
GeneralRe: My vote of 4 PinprofessionalAnoop Kr Sharma27-Jul-13 19:45 
GeneralRe: My vote of 4 PinmemberSsntosh Chouhan27-Jul-13 20:25 
GeneralMy vote of 4 PinmemberMr.Artist27-Jul-13 7:12 
GeneralRe: My vote of 4 PinprofessionalAnoop Kr Sharma30-Oct-13 6:23 
GeneralMy vote of 4 PinprofessionalAmir Mohammad Nasrollahi26-Jul-13 20:34 
GeneralRe: My vote of 4 PinprofessionalAnoop Kr Sharma26-Jul-13 20:52 
QuestionMy Vote of 4 PinmemberCraigDJ26-Jul-13 17:28 
GeneralRe: My Vote of 4 PinprofessionalAnoop Kr Sharma26-Jul-13 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 8 Aug 2013
Article Copyright 2013 by Anoop Kr Sharma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid