从接口获取文件流方式下载excel有两种,这里只展示blob方式,需要后台下载号文件,转换成文件流返回。
参考案例:https://segmentfault.com/a/1190000020540788
文件后缀:MIME(W3CSchool)
axios({
url: '',
method: '',
responseType: 'arraybuffer',
data
}).then(res => {
// application/vnd.ms-excel是赚换的文件格式,根据需要参考上方的MIME链接来确定
let blob = new Blob([res], {type: "application/vnd.ms-excel"})
let objectUrl = URL.createObjectURL(blob) // 创建URL
const link = document.createElement('a')
link.href = objectUrl
link.download = 'xxx.xls' // 自定义文件名
link.click() // 下载文件
URL.revokeObjectURL(objectUrl) // 释放内存
}).catch(err => {
console.log('接口错误')
})