Click here to Skip to main content
15,558,083 members
Articles / Web Development / HTML
Tip/Trick
Posted 26 Jul 2013

Tagged as

Stats

60.4K views
1.2K downloads
14 bookmarked

HTML5 Download Attribute

Rate me:
Please Sign up or sign in to vote.
4.77/5 (19 votes)
8 Aug 2013CPOL1 min read
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:

Image 1

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

HTML
<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:

HTML
<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:

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)


Written By
Student
India India

Comments and Discussions

 
GeneralMy vote of 4 Pin
IslamHossam18-Aug-13 2:51
professionalIslamHossam18-Aug-13 2:51 
GeneralRe: My vote of 4 Pin
Anoop Kr Sharma18-Aug-13 7:51
professionalAnoop Kr Sharma18-Aug-13 7:51 
GeneralMy vote of 2 Pin
Nicolaj Nyvang13-Aug-13 2:32
Nicolaj Nyvang13-Aug-13 2:32 
GeneralRe: My vote of 2 Pin
Anoop Kr Sharma13-Aug-13 9:00
professionalAnoop Kr Sharma13-Aug-13 9:00 
GeneralMy vote of 5 Pin
Karthik. A8-Aug-13 18:35
Karthik. A8-Aug-13 18:35 
GeneralRe: My vote of 5 Pin
Anoop Kr Sharma8-Aug-13 19:29
professionalAnoop Kr Sharma8-Aug-13 19:29 
GeneralMy vote of 5 Pin
Anurag Gandhi28-Jul-13 6:09
professionalAnurag Gandhi28-Jul-13 6:09 
GeneralRe: My vote of 5 Pin
Anoop Kr Sharma28-Jul-13 8:48
professionalAnoop Kr Sharma28-Jul-13 8:48 
GeneralMy vote of 4 Pin
Ssntosh Chouhan27-Jul-13 9:26
Ssntosh Chouhan27-Jul-13 9:26 
GeneralRe: My vote of 4 Pin
Anoop Kr Sharma27-Jul-13 20:45
professionalAnoop Kr Sharma27-Jul-13 20:45 
GeneralRe: My vote of 4 Pin
Ssntosh Chouhan27-Jul-13 21:25
Ssntosh Chouhan27-Jul-13 21:25 
GeneralMy vote of 4 Pin
Mr.Artist27-Jul-13 8:12
Mr.Artist27-Jul-13 8:12 
GeneralRe: My vote of 4 Pin
Anoop Kr Sharma30-Oct-13 7:23
professionalAnoop Kr Sharma30-Oct-13 7:23 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi26-Jul-13 21:34
professionalAmir Mohammad Nasrollahi26-Jul-13 21:34 
GeneralRe: My vote of 4 Pin
Anoop Kr Sharma26-Jul-13 21:52
professionalAnoop Kr Sharma26-Jul-13 21:52 
QuestionMy Vote of 4 Pin
Mitchell J.26-Jul-13 18:28
professionalMitchell J.26-Jul-13 18:28 
GeneralRe: My Vote of 4 Pin
Anoop Kr Sharma26-Jul-13 21:04
professionalAnoop Kr Sharma26-Jul-13 21:04 

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

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