Home  >  Article  >  Web Front-end  >  Use of the mandatory download attribute download in HTML5

Use of the mandatory download attribute download in HTML5

不言
不言Original
2018-06-05 16:10:459353browse

Adding the download attribute to the link allows users to download the file instead of opening it directly with the browser. So far, browsers that support HTML5 have relatively good support for this attribute. Let’s take a closer look below. Let’s analyze the usage examples of the forced download attribute download in HTML5:

The Download attribute of HTML5 is used to force the browser to download the corresponding file instead of opening it. Browsers such as Chrome and Firefox are too powerful, perhaps to enhance the user experience. When the resource file clicked by the user can be recognized by them (for example, pdf will be opened directly in the browser, and media such as mp3 and mp4 will be played directly within the browser) player playback). But sometimes, users actually want to download it directly instead of viewing it on the browser. In this case, they can add this attribute, and the attribute value will rename the downloaded file:
86b4c3c95c15be3f8c2312f1f7020f74Click to download directly and save it as a download.pdf file5db79b134e9f6b82c0b36e0489ee08ed
If you are sure that the user will definitely download this resource, you can add this attribute, and you can also use JS or manually change the file name you want to save.
It is convenient to create a download link in HTML. Just add an 3499910bf9dac5ae3c52d5ede7383485 tag and an href attribute pointing to the file. But some files will not be downloaded (such as images, pdf, txt, doc), instead, they will be opened in the browser.
If your site is server-side, you can configure the .htaccess file so that those files can be downloaded. If your site is hosted by WordPress.com or github pages (static pages), then consider using the download attribute of the 3499910bf9dac5ae3c52d5ede7383485 tag

Use the "Download" attribute
## The #download attribute is part of the HTML5 specification and behaves as a download link rather than a navigation link.
The download attribute also allows you to rename a file that needs to be downloaded. For example, a file is stored on the server. If the file is automatically generated, it is generally named as a combination of system numbers and dashes, such as acme-doc-2.0.1.txt. We can rename this The name of the downloaded file. The file name that users see after downloading can be a better name, such as Acme Documentation (ver. 2.0.1).txt, to increase user experience like this (don’t forget the file extension).

XML/HTML CodeCopy the content to the clipboard

<a href="downloadpdf.php" download="download.pdf">点击直接下载并保存成 download.pdf 文件</a>[object Object]

You can take a look at this demo address: http://tutsplus.github.io /download-attribute/index.html

Some notes:
Firefox considers security issues, the downloaded file must be from its own server or domain name, otherwise it will be browsed Open in the device.
In Chrome and Opear, if the downloaded file is not in a subset of servers or domain names, these browsers will ignore the download attribute. In other words, the file name will not change.

Provide a backup plan:
At the time of writing this article, the download attribute is not implemented in Safari and IE, but IE claims that the implementation of the download attribute is already at the top of the development schedule .

2016512105956946.jpg (600×340)

In the meantime, we can use a fallback solution to be compatible with those browsers. We need to download Modernizr’s download attribute feature test.


2016512110024606.jpg (600×380)

Then add the following script:

JavaScript CodeCopy content to clipboard

if ( ! Modernizr.adownload ) {   
    var $link = $(&#39;a&#39;);   
    $link.each(function() {   
        var $download = $(this).attr(&#39;download&#39;);   
        if (typeof $download !== typeof undefined && $download !== false) {   
      var $el = $(&#39;<p>&#39;).addClass(&#39;download-instruction&#39;).text(&#39;Right-click and select "Download Linked File"&#39;);   
      $el.insertAfter($(this));   
        }   
    });   
}

This script is to test whether the browser supports the download attribute. If the browser does not support it, it will insert a < with the download-instruction class below the 3499910bf9dac5ae3c52d5ede7383485 tag with the download attribute. p> tag, and give text instructions to use right-click to download.


2016512110040425.jpg (600×380)

Related recommendations:


Summary of methods for text alignment in HTML5 Canvas

Html5 title ceiling function

The above is the detailed content of Use of the mandatory download attribute download in HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn