JS front-End Download text file tips

Source: Internet
Author: User

First, HTML and file download

If you want to trigger the download of some resources directly on the front-end side, the easiest and quickest way is to use HTML5 native download properties, such as:

<a href="large.jpg"download> Downloads </a>

But obviously, if you use HTML attributes purely to implement a file download (rather than a browser opening or browsing), there is nothing you can do about dynamic content.

For example, when we share the page, want to share the image is the real-time content of the page, at this time, this picture is dynamic, pure HTML is obviously unable to meet our needs, with the help of JS and some other HTML5 features, for example, the page element to the canvas top, and then to the image to download.

  Whether to support the monitoring of the Download property: to monitor whether the current browser supports download attributes, a line of JS code can be, as follows:

var ' Download '  in Document.createelement ('a');

Second, the use of HTML5 blob to achieve text information file download

The principle is actually very simple, we can convert the text or the JS string information with the BLOB into binary, and then, as <a> the attributes of the element, href with download attributes, to achieve the download.

The code is also relatively simple, as shown below (compatible with Chrome and Firefox):

varFundownload =function (content, filename) {//Create a hidden downloadable link    varElelink = Document.createelement ('a'); Elelink.download=filename; EleLink.style.display='None'; //convert character content to blob address    varBlob =NewBlob ([content]); Elelink.href=Url.createobjecturl (BLOB); //Trigger ClickDocument.body.appendChild (Elelink);    Elelink.click (); //and then removeDocument.body.removeChild (Elelink);};

This refers to the content text or string content that needs to be downloaded, filename referring to the file name downloaded to the system.

The JS code that triggers the download is just a few lines:

Button.addeventlistener ('click', function () {    'test.html  ');    });

Not only .html files, .txt and .json so on, as long as the content is text files, can be used to achieve the download of this small trick.

In the Chrome browser, the simulated click creates the <a> element even if not append to the page, also can trigger the download, but in the Firefox browser does not have, therefore, the above funDownload() method has one appendChild and removeChild the processing, is to be compatible with Firefox browser.

Instance:

varEletextarea = Document.queryselector ('textarea');varElebutton = Document.queryselector ('input[type= "button"]');//Download File MethodvarFundownload =function (content, filename) {varElelink = Document.createelement ('a'); Elelink.download=filename; EleLink.style.display='None'; //convert character content to blob address    varBlob =NewBlob ([content]); Elelink.href=Url.createobjecturl (BLOB); //Trigger ClickDocument.body.appendChild (Elelink);    Elelink.click (); //and then removeDocument.body.removeChild (Elelink);};if('Download' inchDocument.createelement ('a')) {    //Download as test.html fileElebutton.addeventlistener ('Click', function () {fundownload (Eletextarea.value,'test.html'); });} Else{Elebutton.onclick=function () {alert ('Browser does not support'); };}

JS front-End Download text file tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.