JS Create file download

Source: Internet
Author: User
Tags base64
var hrefvalue = "Data:application/vnd.ms-excel;base64," + wbout;
anchor.download = filename + '. txt ';    Format random
anchor.href = hrefvalue;
Server-Side

The new feature of HTML5 adds a download attribute to the a tag, fill in the filename in the download attribute, and href fills in the address you want to jump (jump page download page, jump file download file, such as you are a little PDF directly open, now you can download a little), You can download the corresponding file so that when you click on the link, the browser does not open the link to the file, but instead download (currently only Chrome, Firefox and opera support).

<a href= "Www.xxx.com/document/a.txt" download= "Newname.txt" >
Browser End

Method One, Datauri, more common is the picture of SRC, such as:
explain Datauri:

First, the URL is not just a common kind, there are also used to transmit data,
The example above contains the following sections:
Data-contract name to obtain
Image/png-Data type name
Base64-coding method of data
R0lgoxxxxx ...-encoded data
: , ; -separator symbol specified by data URI scheme
It means to transmit an IMG file encoded in base64 what is base64

Simply put, it translates some 8-bit data into standard ASCII characters, and JS and PHP can be base64 processed
Citation: http://www.alloyteam.com/2014/01/use-js-file-download/

function DownloadFile (fileName, content) {
    var aLink = document.createelement (' a ');
    var blob = new blob ([content]);
    var evt = document.createevent ("htmlevents");
    Evt.initevent ("Click", False, False)//initevent without the latter two parameters in FF will be an error, thanks to Barret Lee's feedback
    alink.download = fileName;
    Alink.href = Url.createobjecturl (BLOB);
    Alink.dispatchevent (EVT);
}

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.