流转化方法
//流转化
export function exportSearchList(dowLoadFileName, result) {
const blob = new Blob([result]);
const fileName = dowLoadFileName + '.xls';
// 判断浏览器
var brower = '';
if (navigator.userAgent.indexOf('Edge') > -1) {
brower = 'Edge';
}
if ('download' in document.createElement('a')) {
// 非IE下载
if (brower == 'Edge') {
navigator.msSaveBlob(blob, fileName);
return;
}
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
// 释放URL 对象
document.body.removeChild(elink);
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
}
第一个参数为文件名,第二个参数为接口请求回来的blob流
在自己模块引入上述js文件,然后在当初方法中调用即可
// 导出
Export(){
axios({
method: 'post',
url: 'kkkk',
data: {},
headers:{
'Content-type': 'application/json',
},
responseType: 'blob'
}).then(res=>{
// console.log(res);
this.exportSearchList('deviceBorrow', res.data);
});
},
注解一下:
如果使用window.URL.createObjectURL这种方式的化,是不兼容IE的。
如果要考虑兼容性问题,不建议使用下面的方式
axios({
method: 'post',
url: 'kkkk',
data: {},
headers:{
'Content-type': 'application/json',
},
responseType: 'blob'
}).then(res=>{
// console.log(res);
let url = window.URL.createObjectURL(res)
let link = document.createElement('a')
link.href = url
link.setAttribute('download', `单据-${this.searchForm.exportDateStr}.zip`)
link.click()
});