
responseType: 'blob'
axios({
responseType: 'blob'
}).then((res) => {
console.log(`下载成功`, res)
//存储文件流
const fileData = res
//实例化一个blob数据
const blobData = new Blob([fileData])
// 设置下载后的文件名
const fileName = 'fileName.xlsx'
//对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
//IE10以上支持blob但是依然不支持download
if ('download' in document.createElement('a')) {
//支持a标签download的浏览器
//创建a标签
const link = document.createElement('a')
//a标签添加属性
link.download = fileName
link.style.display = 'none'
link.href = URL.createObjectURL(blobData)
document.body.appendChild(link)
//模拟点击自动下载
link.click()
//释放url
URL.revokeObjectURL(link.href)
//释放标签
document.body.removeChild(link)
} else {
//其他浏览器
navigator.msSaveBlob(blob, fileName)
}
})
.catch((err) => {
console.log(`下载错误`, err)
})
END.