须将axios 配置中的
responseType
设置为arraybuffer
text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel
// 下载文件
async exportConsumeList() {
try {
let res = await exportConsumeList()
var blob = new Blob([res], {
type: 'text/csv,charset=UTF-8',
})
if ('download' in document.createElement('a')){
// 非IE下载
var downloadElement = document.createElement('a')
var href = window.URL.createObjectURL(blob) //创建下载的链接
downloadElement.href = href
downloadElement.download = '用户数据.csv' //下载后文件名
document.body.appendChild(downloadElement)
downloadElement.click() //点击下载
document.body.removeChild(downloadElement) //下载完成移除元素
window.URL.revokeObjectURL(href) //释放掉blob对象
}else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
} catch (error) {
console.log(error)
}
},