<button type= "button" onclick= "Download ()" > Export </button>functionDownload () {varurl = ' Download/?filename=aaa.txt '; varXHR =NewXMLHttpRequest (); Xhr.open (' GET ', url,true);//You can also use the Post method, depending on the interfaceXhr.responsetype= "blob";//return type blob //defines the processing function for request completion, which can also be added to the Load box/disable download button logic before the requestXhr.onload=function () { //Request Complete if( This. Status = = 200) { //back to varBlob = This. Response; varReader =NewFileReader (); Reader.readasdataurl (BLOB); //convert to Base64, can be placed directly into a expression hrefReader.onload=function(e) {//conversion Complete, create a label for download varA = document.createelement (' A '); A.download= ' data.xlsx '; A.href=E.target.result; $("Body"). Append (a);//fix not trigger click in FirefoxA.click (); $ (a). Remove (); } } }; //send an AJAX requestxhr.send ()}
Original link: https://my.oschina.net/watcher/blog/1525962
Ajax Way to download files