General download is directly open a link on the line.
var URL = ' XXXX ';
window.open (URL)
In fact, this will have some problems:
1. The browser prohibits opening a new window, resulting in the inability to download
So how to solve it?
Such
1 < href= "http://somehost/somefile.zip" download= "Filename.zip" >Download file</a>
Note the Download property, which describes the download, and the file name: Filename.zip
So how to download asynchronously?
JS Code:
1 varMydownload =function(){};2Mydownload.prototype = {3 //sync Download, just turn the above example into JS4Downloadfunction(URL, filename) {5 varA = document.createelement (' A ');6A.href =URL;7A.download =filename;8 A.click ();9 },Ten One //according to the Blob object to download ADownloadbyblob:function(blod,name) { -Console.log (' Blod ', Blod); -Name = Name | |Blod.name; the varFileURL = window. Url.createobjecturl (Blod);//Convert to an accessible URL - This. Download (FileURL, name);//Download -Window. Url.revokeobjecturl (FileURL);//release URL after download - }, + - //asynchronous Data= ' name=mynam&age=20 ' +Ajax:function(URL,DATA,METHOD,TYPE,SUCCESSFUC,ERRORFUC) { AMethod = Method | | ' GET '; atdata = Data | | ‘‘; -Type = Type | | ' Blob '; - varRequest =NewXMLHttpRequest (); - if(window. XMLHttpRequest) { -request=NewXMLHttpRequest (); - } in Else if(window. ActiveXObject) { -request=NewActiveXObject ("Microsoft.XMLHTTP"); to}Else{ + return false; - } the Request.open (method, url); *Request.responsetype =type; $ //When the request loads, check whether it is successfulPanax NotoginsengRequest.onload =function() { - if(Request.status = = 200) { the if(SUCCESSFUC) { + varfilename = Request.getresponseheader (' content-disposition '); A if(filename) { the varindex = filename.indexof (' filename= '); + if(Index!==-1){ -filename = filename.substring (index+9); $ } $ } - successfuc (request.response,filename) - } the}Else { - if(SUCCESSFUC) {Wuyi successfuc (request.response) the } - } Wu } -Request.onerror =function(Error) { About //Also deal with the case, the entire request fails to begin with $ //This is probably a network error, so reject the promise with an appropriate message - if(ERRORFUC) { - errorfuc (error.response) - } A }; + //Send the request theRequest.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); - request.send (data); $ }, the the //Asynchronous Download No promise theDownloadasync:function(url,name,data,method) { the varSelf = This; - This. Ajax (Url,data,method, ' blob ',function(blob,filename) { inSelf.downloadbyblob (blob,name| |filename); the }); the }, About the //asynchronous download using Promise theDownloadpromise:function(url,name,data,method) { the if(window. Promise) { + varSelf = This; - return NewPromise (function(Resolve, reject) { theConsole.log ( This, ' this ');BayiSelf.ajax (Url,data,method, ' blob ',function(response,filename) { the Resolve ({ the Data:response, - Filename:filename - }); the},function(response,filename) { the reject ({ the Data:response, the Filename:filename - }); the }) the}). Then (function(JSON) { the Self.downloadbyblob (json.data,json.filename);94 });; the}Else{ the Self.downloadasync (url,data,method); the }98 } About}
Code for PHP:
1 Ini_set(' Memory_limit ', ' 64M ');2 $file= "Mylittlevader.jpg";3 Header(' Content-type:application/octet-stream ');4 Header(' Content-disposition:attachment; Filename= '.$file);//Set file name5 Header(' Content-length: '.filesize($file));6 ReadFile($file);
Html:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge,chrome=1">6 <Metaname= "Viewport"content= "Width=device-width">7 8 <title>Asynchronous Download Example</title>9 </Head>Ten One <Body> A <H1>Asynchronous Download Example</H1> - <ahref="#"onclick= ' Downjs.downloadpromise ("download.php") '>Downloadjs</a> - </Body> the - <Scripttype= "Text/javascript"src= "./download.js"></Script> - <Script> - varDownjs= Newmydownload (); + </Script> - </HTML>
The general use of download can be both, can directly:
1 downjs.download (URL)
Reference:
1190000005863250
Promise Download
JavaScript Asynchronous Download Promise implementation