注意
1.一定要在请求的头部添加blob来获取二进制流
2.要确定接口请求方式params,data
3.通过建立原数a 触发 href 来进行下载事件
请求是时候一定要请求头添加 responseType: ‘blob’, 来获取二进制流
axios({
method: 'post',
responseType: 'blob',
url: process.env.BASE_API + '接口地址',
params: form
})
.then(response => {
console.log(response.data, 'cg')
var timestamp = Date.parse(new Date()) // 获取当前时间戳
const link = document.createElement('a')// 创建下载元素
const url = window.URL.createObjectURL(new Blob([response.data]))// 创建下载的链接
link.style.display = 'none'
link.href = url
link.setAttribute('download', '区域销售导出-' + timestamp + '.xls')// 名字
document.body.appendChild(link)
link.click()// 点击下载
link.remove()// 下载完成移除元素
})
.catch(response => {
})