Quick Tip: Using the HTML5 Download Attribute

Creating a download link in HTML is straightforward; add an anchor tag and point to the file within the “href” attribute. Some file types, however, won’t be downloaded; they’ll be opened in the browser. In this case consider using the “download” attribute.

If you have server-side access to your website there are some workarounds you can use, such as configuring the .htaccess, to download these files directly. If your site is hosted with a free service like WordPress.com, Blogspot, or perhaps Github pages which don’t allow you to do so, consider using the download attribute.

Using the HTML5 Download Attribute

The download attribute is part of the HTML5 spec and expresses a link as download link rather than a navigational link.

Syntax

1
<a href="download/acme-doc.txt" download>Download Text</a>

The download attribute allows you to optionally rename the file name upon downloading. When the file resides on the server, especially if it’s been automatically generated, this can be ideal for giving users a file with a more sensible name when downloaded, perhaps like: Acme Documentation (ver. 2.0.1).txt (not forgetting the file extension).

Here’s how that would look in practice:

1
<a href="download/acme-doc-2.0.1.txt" download="Acme Documentation (ver. 2.0.1).txt">Download Text</a>

Give it a try on the demo page, and you should find the file downloaded with the name specified in the download attribute.

the html download procedurethe html download procedurethe html download procedure

A Couple of Notes:

  • Firefox only allows users to download files of the same origin due to a security concern. The file must come from your own server or domain name, otherwise it will be opened in the browser. 
  • While downloading cross-origin files is allowed in Chrome and the latest Opera (with Chromium/Blink), they will both ignore the attribute value. In other words, the file name will remain unchanged.

The download attribute can also be used with the less common <area> element.

Providing Fallback for Internet Explorer

The download attribute has not been implemented in Internet Explorer, though it is supported by Edge. 

browser support for the HTML5 download attribute, from caniuse.combrowser support for the HTML5 download attribute, from caniuse.combrowser support for the HTML5 download attribute, from caniuse.com
browser support for the download attribute, from caniuse.com

In order to make things bullet proof we can add a decent fallback, such as providing extra instructions below the download link for non-supporting browsers. To do so, we will need to download Modernizr with the download feature test included.

modernizr script with download attribute featuremodernizr script with download attribute featuremodernizr script with download attribute feature
Configure Modernizr build.

Then we can add the following script.

1
if ( ! Modernizr.adownload ) {
2
	var $link = $('a');
3

4
	$link.each(function() {
5
		var $download = $(this).attr('download');
6
	
7
		if (typeof $download !== typeof undefined && $download !== false) {
8
      var $el = $('<div>').addClass('download-instruction').text('Right-click and select "Download Linked File"');
9
      $el.insertAfter($(this));
10
		}
11

12
	});
13
}

The script will test whether the browser supports the download attribute; if not it will append a new <div> with the class for styling purposes as well as the instruction text, and insert it immediately below any link which has been furnished with the download attribute.

backup download instructionsbackup download instructionsbackup download instructions

Conclusion

The HTML5 download attribute makes handling download links very convenient for anyone who has no access to server-side configuration.

Learn More HTML


This content originally appeared on Envato Tuts+ Tutorials and was authored by Thoriq Firdaus

Creating a download link in HTML is straightforward; add an anchor tag and point to the file within the “href” attribute. Some file types, however, won’t be downloaded; they’ll be opened in the browser. In this case consider using the “download” attribute.

If you have server-side access to your website there are some workarounds you can use, such as configuring the .htaccess, to download these files directly. If your site is hosted with a free service like WordPress.com, Blogspot, or perhaps Github pages which don’t allow you to do so, consider using the download attribute.

Using the HTML5 Download Attribute

The download attribute is part of the HTML5 spec and expresses a link as download link rather than a navigational link.

Syntax

1
<a href="download/acme-doc.txt" download>Download Text</a>

The download attribute allows you to optionally rename the file name upon downloading. When the file resides on the server, especially if it’s been automatically generated, this can be ideal for giving users a file with a more sensible name when downloaded, perhaps like: Acme Documentation (ver. 2.0.1).txt (not forgetting the file extension).

Here’s how that would look in practice:

1
<a href="download/acme-doc-2.0.1.txt" download="Acme Documentation (ver. 2.0.1).txt">Download Text</a>

Give it a try on the demo page, and you should find the file downloaded with the name specified in the download attribute.

the html download procedurethe html download procedurethe html download procedure

A Couple of Notes:

  • Firefox only allows users to download files of the same origin due to a security concern. The file must come from your own server or domain name, otherwise it will be opened in the browser. 
  • While downloading cross-origin files is allowed in Chrome and the latest Opera (with Chromium/Blink), they will both ignore the attribute value. In other words, the file name will remain unchanged.
The download attribute can also be used with the less common <area> element.

Providing Fallback for Internet Explorer

The download attribute has not been implemented in Internet Explorer, though it is supported by Edge. 

browser support for the HTML5 download attribute, from caniuse.combrowser support for the HTML5 download attribute, from caniuse.combrowser support for the HTML5 download attribute, from caniuse.com
browser support for the download attribute, from caniuse.com

In order to make things bullet proof we can add a decent fallback, such as providing extra instructions below the download link for non-supporting browsers. To do so, we will need to download Modernizr with the download feature test included.

modernizr script with download attribute featuremodernizr script with download attribute featuremodernizr script with download attribute feature
Configure Modernizr build.

Then we can add the following script.

1
if ( ! Modernizr.adownload ) {
2
	var $link = $('a');
3
4
	$link.each(function() {
5
		var $download = $(this).attr('download');
6
	
7
		if (typeof $download !== typeof undefined && $download !== false) {
8
      var $el = $('<div>').addClass('download-instruction').text('Right-click and select "Download Linked File"');
9
      $el.insertAfter($(this));
10
		}
11
12
	});
13
}

The script will test whether the browser supports the download attribute; if not it will append a new <div> with the class for styling purposes as well as the instruction text, and insert it immediately below any link which has been furnished with the download attribute.

backup download instructionsbackup download instructionsbackup download instructions

Conclusion

The HTML5 download attribute makes handling download links very convenient for anyone who has no access to server-side configuration.

Learn More HTML


This content originally appeared on Envato Tuts+ Tutorials and was authored by Thoriq Firdaus


Print Share Comment Cite Upload Translate Updates
APA

Thoriq Firdaus | Sciencx (2015-04-25T07:37:59+00:00) Quick Tip: Using the HTML5 Download Attribute. Retrieved from https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/

MLA
" » Quick Tip: Using the HTML5 Download Attribute." Thoriq Firdaus | Sciencx - Saturday April 25, 2015, https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/
HARVARD
Thoriq Firdaus | Sciencx Saturday April 25, 2015 » Quick Tip: Using the HTML5 Download Attribute., viewed ,<https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/>
VANCOUVER
Thoriq Firdaus | Sciencx - » Quick Tip: Using the HTML5 Download Attribute. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/
CHICAGO
" » Quick Tip: Using the HTML5 Download Attribute." Thoriq Firdaus | Sciencx - Accessed . https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/
IEEE
" » Quick Tip: Using the HTML5 Download Attribute." Thoriq Firdaus | Sciencx [Online]. Available: https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/. [Accessed: ]
rf:citation
» Quick Tip: Using the HTML5 Download Attribute | Thoriq Firdaus | Sciencx | https://www.scien.cx/2015/04/25/quick-tip-using-the-html5-download-attribute/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.