JS下载⽂件,解决⽂件直接在浏览器打开的情况JS下载⽂件,解决⽂件直接在浏览器打开的情况:
function downloadfile(url, file_name) {
var xmlHttp = null;
if (window.ActiveXObject) {
js arguments// IE6, IE5 浏览器执⾏代码
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执⾏代码
xmlHttp = new XMLHttpRequest();
}
//2.如果实例化成功,就调⽤open()⽅法:
if (xmlHttp != null) {
xmlHttp.open("get", url, true);
xmlHttp.send();
}
function doResult() {
if (adyState == 4) { //4表⽰执⾏完成
if (xmlHttp.status == 200) { //200表⽰执⾏成功
//引⽤js库:download.js
sponseText, file_name, "text/plain");
}
}
}
}
download.js:
//download.js v3.0, by dandavis; 2008-2014. [CCBY2] see danml/download.html for tests/usage
// v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime
// v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs
// v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support
// data can be a string, Blob, File, or dataURL
function download(data, strFileName, strMimeType) {
var self = window, // this script is only for
u = "application/octet-stream", // this default mime also triggers iframe downloads
m = strMimeType || u,
x = data,
D = document,
a = D.createElement("a"),
z = function(a){return String(a);},
B = self.Blob || self.MozBlob || self.WebKitBlob || z,
BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
fn = strFileName || "download",
blob,
b,
ua,
fr;
//if(typeof B.bind === 'function' ){ B=B.bind(self); }
if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "l") to act as a callback
x=[x, m];
m=x[0];
x=x[1];
}
//go ahead and download dataURLs right away
if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){
return navigator.msSaveBlob ?  // IE10 can't do a[download], only Blobs:
navigator.msSaveBlob(d2b(x), fn) :
saver(x) ; // everyone else can save dataURLs un-processed
}//end if dataURL passed?
try{
blob = x instanceof B ?
x :
new B([x], {type: m}) ;
}catch(y){
if(BB){
b = new BB();
b.append([x]);
blob = b.getBlob(m); // the blob
}
}
function d2b(u) {
var p= u.split(/[:;,]/),
t= p[1],
dec= p[2] == "base64" ? atob : decodeURIComponent,
bin= dec(p.pop()),
mx= bin.length,
i= 0,
uia= new Uint8Array(mx);
for(i;i<mx;++i) uia[i]= bin.charCodeAt(i);
return new B([uia], {type: t});
}
function saver(url, winMode){
if ('download' in a) { //html5 A[download]
a.href = url;
a.setAttribute("download", fn);
a.innerHTML = "";
D.body.appendChild(a);
setTimeout(function() {
a.click();
veChild(a);
if(winMode===true){setTimeout(function(){ vokeObjectURL(a.href);}, 250 );}            }, 66);
return true;
}
//do iframe dataURL download (old ch+FF):
var f = D.createElement("iframe");
D.body.appendChild(f);
if(!winMode){ // force a mime that will download:
url="data:"+place(/^data:([\w\/\-\+]+)/, u);
}
f.src = url;
setTimeout(function(){ veChild(f); }, 333);
}//end saver
if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
return navigator.msSaveBlob(blob, fn);
}
if(self.URL){ // simple fast and modern way using Blob and URL:
saver(ateObjectURL(blob), true);
}else{
// handle non-Blob()+non-URL browsers:
if(typeof blob === "string" || structor===z ){
try{
return saver( "data:" +  m  + ";base64,"  +  self.btoa(blob)  );
}catch(y){
return saver( "data:" +  m  + "," + encodeURIComponent(blob)  );
}
// Blob but not URL:
fr=new FileReader();
};
return true;
} /* end download() */

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。