请求后使用blog下载
async handleExport (row) {
const params = {"id": row.id,}
const res = await this.$store.dispatch('AAA/BBB', params)
if (res) {
this.$message({
message: '正在下载',
type: 'success'
})
const blob = new Blob([res.data], { type: res.data.type }) //创建一个blog
let i = res['content-disposition'].indexOf("=")
let filename = res['content-disposition'].substr(i + 1) //'下载文件.doc';
if (window.navigator && window.navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename)
} else {
const a = document.createElement('a') //创建一个a链接
const url = window.URL.createObjectURL(blob)
a.href = url //创建a连接的href
a.download = filename //设置名称
a.click() //点击事件
window.URL.revokeObjectURL(url) //释放url对象
}
}
},
1、window.navigator
对象包含有关访问者浏览器的信息,window.navigator对象在编写时也可以不使用 window 这个前缀。
2、window.navigator.msSaveBlob
使用Blob构造函数可以直接在客户端上创建和操作Blob。
用法:
(1)msSaveBlob:只提供一个保存按钮
window.navigator.msSaveBlob(blobObject, 'msSaveBlob_testFile.txt');
(2)msSaveOrOpenBlob:提供保存和打开按钮
window.navigator.msSaveOrOpenBlob(blobObject, 'msSaveBlobOrOpenBlob_testFile.txt');