前端下载后端传过来的文件流
首先axios请求中的responseType: ‘blob’
dataDownload(res) {
const fileName = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]) // 文件名
const data = res.data // Blob数据对象
const uA = window.navigator.userAgent
const isIE =
/msie\s|trident\/|edge\//i.test(uA) &&
!!('uniqueID' in document || 'documentMode' in document || 'ActiveXObject' in window || 'MSInputMethodContext' in window)
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
// 兼容IE
if (isIE) {
navigator.msSaveBlob(new Blob([data]), fileName)
} else {
link.click()
}
},