流文件只需处理对格式即可
Axios({
url: url,
method: 'get',
params: {},
responseType: 'blob' // 改参数会以blob流格式返回
}).then((res) => {
let blob = new Blob([res.data])
let url = URL.createObjectURL(blob)
// 下载该blob链接
downloadFile(url, 'abc.text')
})
// 下载方法
function downloadFile (downUrl, fileName) {
const aLinkUrl = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
aLinkUrl.href = downUrl
aLinkUrl.download = fileName
const clickAlink = (obj) => { // 模拟点击
const ev = document.createEvent('MouseEvents')
ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
obj.dispatchEvent(ev)
}
clickAlink(aLinkUrl)
}
网友评论