1.util.js封装
export function dataToFile (params) {
let type = params.type;
let fileName = params.fileName;
let data = params.data;
// 兼容 IE
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(new Blob([data]), fileName);
} else {
// 非IE 浏览器
const url = window.URL.createObjectURL(new Blob([data], { type }));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `${fileName}`);
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(url); // 释放内存
}
}
2.axios中,加入请求类型{responseType: 'arraybuffer'}
3.页面请求