await axios.get(
`${import.meta.env.VITE_API_URL}/cloud-data-acquisition/task/dataConfigCard/export?pinnerId=`+param3.pinnerId,
{headers:{'authorization':`Bearer ${Cookies.get('token-base')}`}})
.then((res)=>{
// 转换为 Blob
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// 触发下载
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = '数据采集信息.xlsx';
a.click();
// 释放临时的 URL
setTimeout(() => URL.revokeObjectURL(url), 100);
})
.catch((err)=>{
console.log(err)
})
关于前端文件部分踩的坑,一开始我用
// const url = 'http://localhost:8915/task/dataConfigCard/export?pinnerId='+param3.pinnerId; // 替换为你的后端API地址
// window.location.href = url; // 或者使用axios等发送GET请求来触发下载
这种打开地址栏的方式去触发后端导出,但是后来我觉得,如果项目上线的话,岂不是很麻烦,还是要前端代理后再转发给后端
这是我踩的第一个坑
第二个坑,要有返回类型,上面我也没写,突然找不到代码了,返回类型要是blob,不然还是要出错。
然后 关于cookie相关的取值,我这是取了一个token出来,因为我要访问后端
`${import.meta.env.VITE_API_URL} 这是取代理的地址
EasyExcel 学习之 导出 “WPS 表格在试图打开文件时遇到错误”_could not initialize class org.openxmlformats.sche-优快云博客