1、通过http请求,拿到字节流res
2、创建a标签
3、指定a标签的download属性,作为文件下载后的名字
4、设置a标签隐藏
5、将字节流创建Blob对象
6、给该blob对象创建一个临时地址,a标签的href属性指向他
7、将a标签插入到DOM中
8、触发a标签的点击事件,去下载
9、从DOM中移除a标签
downReportWord(params).then(res => {
if (res) {
const elink = document.createElement('a');
elink.download = '专报.docx';
elink.style.display = 'none';
const blob = new Blob([res], { type: 'application/x-msdownload' });
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
} else {
this.$message.error('导出异常请联系管理员');
}
})
注意:
请求是一定要带responseType:'blob'