需求是按查询条件导出

后端提供的接口是get方式,并且有入参

接口返回的是文档流

最简单的方式是利用a标签href直接打开拼接后的get地址。
也可以用window.open()打开拼接好的url,但是后重新打开一个tab页,虽然是瞬间的但是体验不好
经过谷歌查询以后采用了以下方式
axios.get(`/insuranceOrder/exportExcel`, { //url: 接口地址
params: this.queryParam,
responseType: `arraybuffer` //一定要写
})
.then(res => {
if (res.status == 200) {
let blob = new Blob([res.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
//word文档为application/msword,pdf文档为application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
});
let objectUrl = URL.createObjectURL(blob);
let link = document.createElement("a");
let fname = `保险询价列表`; //下载文件的名字
link.href = objectUrl;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();
} else {
this.$message({
type: "error",
message: "导出失败"
})
}
})
网友评论