写法1
function handleExport() {
// 调用接口获取返回的文件流
exportReport(params).then((res) => {
const blobData = new Blob([res], {
type: 'application/vnd.ms-excel',
});
//创建一个空的 a 标签
const downloadA = document.createElement('a');
// 将 blob 文件转化为 url 格式并赋值给a标签的 href 属性
downloadA.href = URL.createObjectURL(blobData);
// 给a标签设置下载链接并命名
downloadA.setAttribute('download', '车型表.xlsx');
// 点击事件触发 href
downloadA.click();
});
}
写法2
function handleExport() {
// 调用接口获取返回的文件流
exportReport(params).then((res) => {
const blob = new Blob([res], { type: "application/vnd.ms-excel" });
if ("download" in document.createElement("a")) {
// 非IE下载
const elink = document.createElement("a");
elink.download = '车型表.xlsx';
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
setTimeout(() => {
Message.success('下载成功')
}, 500);
} else {
// IE10+下载
navigator.msSaveBlob(blob, '车型表.xlsx');
Message.success('下载成功')
setTimeout(() => {
Message.success('下载成功')
}, 500);
}
});
}