这里是将后台返回的数据生成表格下载下来
如果所示,后台返回的是乱码
![](https://img.haomeiwen.com/i14620593/f91514ff7cc6c79f.png)
这时我们在请求接口的时候 需要加上下面的代码就可以了
responseType: "blob" // 解决文件下载乱码问题
![](https://img.haomeiwen.com/i14620593/e32be59ad0f97acb.png)
这时返回的就是一个Blob对象了
![](https://img.haomeiwen.com/i14620593/8987dea9d44fd258.png)
然后 将后台得到的Blob下载下来就可以了
.then((res) => {
console.log(res);
const link = document.createElement('a')
let blob = new Blob([res.data], {type:'application/vnd.ms-excel'});
const fileName = "商品管理.xls";
link.href = URL.createObjectURL(blob)
link.download = fileName;
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href); // 释放URL 对象
document.body.removeChild(link)
}).catch(error => {
this.$message({
message: '导出失败',
type: 'success',
duration: 3000
})
})
网友评论