注意求类型:responseType: 'blob'
axios({
url: url,
method: 'post',
data: data,
responseType: 'blob'
}).then(res => {
let blob = new Blob([res.data])
let url = window.URL.createObjectURL(blob)
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
// 文件名一般是在res.headers里:content-disposition;fileName=xxxxxxxxxx.csv,这个让后端统一规定文件名怎么放前端就怎么取就行
letfilename = res.headers['content-disposition'].split('=')[1] || '测试.xlsx'
link.setAttribute('download', decodeURIComponent(filename)) // 解码,这里也可以自定义下载的文件名字,如link.setAttribute('download', 'xxxxxdownload.xls')
document.body.appendChild(link)
link.click() //用新窗口打开window.open(link.click()),但是下载完成后不会先get请求那样自动关闭窗口
})
.catch(error => {
console.log(error)
})