项目新增一个导出功能,后端写了个post接口,半路接手的项目,框架已经由大佬写好了,以为只要按照固有模式写就好了,然而发现提交错误。只能请教大佬,自写一个请求
export const fetchPostDownload = (url, params = {}) => {
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: url,
data: params,
responseType:'arraybuffer',//把文件设置为arraybuffer 文件流
}).then((response) => {
console.log(response)
let blob = new Blob([response.data],{type:"application/vnd.ms-excel;charset=UTF-8"});//type在headers 的content-type获取
const elink = document.createElement('a')
elink.download = new Date().getFullYear()+'-'+(new Date().getMonth()+1)+'-'+new Date().getDate()+'.xls';//文件名
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
})
})
}
事情结束了吗?并没有
发现respones要么等于undefined 要么等于flase,跑去看调试器,发现框架定义的axios判断拦截 把数据拦截下来了,然后改判断条件。所以会f12还是很重要的事情啊