注意两点
1、responseType: ‘blob’,一定不能少,不然数据解析不出来,乱码
2、res一定要根据流的位置进行更改
export function exportMethod() {
axios({
method:'get',
url: url+'params',
// responseType: 'blob',一定不能少,不然数据解析不出来,乱码
responseType: 'blob'
}).then((res) => {
//下面的res一定要根据流的位置进行更改
let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
//兼容IE10
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, '报表统计');
}else{
const link = document.createElement('a')
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = '报表统计' //下载的文件名
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
}).catch(error => {
console.log(error)
})
}