不废话,直接上代码!
axios({
method: 'post',
url: exportCustomMItemDataWithLine,
data: params,
responseType: 'blob'
}).then(res => {
if (!res) {
return
}
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
const fileName = res.headers['content-disposition'].split('=')[1]
let url = ''
let isBlob = false
const errMsg = '下载出错,文件数据无法识别!'
let data = blob
if (data instanceof Blob) {
isBlob = true
url = window.URL.createObjectURL(data)
} else if (typeof data == 'string') {
// base64编码
url = data
} else {
Message.error(errMsg)
return
}
if ('download' in document.createElement('a')) {
// 非IE下载
const tmpLink = document.createElement('a')
tmpLink.download = fileName || ''
tmpLink.style.display = 'none'
tmpLink.href = url
document.body.appendChild(tmpLink)
tmpLink.click()
window.URL.revokeObjectURL(tmpLink.href)
document.body.removeChild(tmpLink)
} else {
// IE10+下载
if (isBlob) {
window.navigator.msSaveBlob(data, fileName)
} else {
console.log(errMsg)
return
}
}
})