vue.js, reactjs 一般,使用js来下载文件。
而,后段一般返回数据流。
这时候,IE用Blob处理,chrome创建个超连接来处理.
s.handleBlob=function(data,fileName){
var blob = new Blob([data],{type: 'application/[图片][图片]vnd.ms-excel'});
let name=fileName+'-'+s.transferTimeToString(new Date())+'.xls';//扩展名
if('msSaveOrOpenBlob' in navigator){//兼容ie
window.navigator.msSaveOrOpenBlob(blob,name);
}else{
let downLoadAllUrl =window.URL.createObjectURL(blob);
var eleLink = document.createElement('a');
eleLink.download = name;
eleLink.style.display = 'none';
eleLink.href = downLoadAllUrl;
document.body.appendChild(eleLink);
eleLink.click();
document.body.removeChild(eleLink);
}
}
fetch(exportBillURL, newOptions).then(response => response.blob())
.then(blob => {
if (window.navigator.msSaveBlob){
try{
var blobObject = new Blob([blob]);
window.navigator.msSaveBlob(blobObject, "billing.csv");
}catch(e){
console.log(e);
}
}else{
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
a.href = url;
a.download = "billing.csv";
a.click();
}
});