首先,感谢 master女士 提供的思路,找了好久,最后实测很好用!
这是后端接口返回的数据流:
首先需要在请求接口方法里定义 responseType
api.js
// 导出
export function teamandmember_downloadTeam(data) {
return request({
url: "business/teamsanwan/downloadTeam.do",
method: "post",
data,
responseType: 'blob' // 重点代码
});
}
接着对数据流直接进行处理就可以了。
teamandmember_downloadTeam(params).then((res) => {
let blob = new Blob([res], { type: "application/vnd.ms-excel;charset=utf-8"} ); // 重点代码
let a = document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.setAttribute("download", `xxx_${new Date().getTime()}.xlsx`); // 表名,自定义
a.click(); // 执行下载
window.URL.revokeObjectURL(a.href); // 释放ur
}).catch((err) => { // 错误处理
conosle.log(err);
}