download () {
let url ="" ;
const link = document.createElement("a"); //创建个a标签
link.href = url;
// link.download = fileName;//重命名
link.click();
URL.revokeObjectURL(url); //释放内存
},
主要就是设置responseType: 'blob'
,缺点是ajax下载的话你在浏览器看不到进度显示
testdow(){
var conf = {responseType: 'blob'}
$http.get("",conf).then(data => {
this.downloadByBlob(data, "xls", "test");
}).catch(error => {
});
},
downloadByBlob(data, fileType, fileName) {
try {
let url = window.URL.createObjectURL(data);
let eleLink = document.createElement('a');
eleLink.href = url;
eleLink.download = `${fileName}.${fileType}`;
document.body.appendChild(eleLink);
eleLink.click();
window.URL.revokeObjectURL(url);
} catch (error) {
console.error('download function error!', error);
}
}