[英]Can't download excel file from api - xlsx/csv
我从 api 将工作簿作为 arraybuffer 返回,然后尝试使用 blob 在反应端下载。
它下载文件,但 csv 文件有一堆数字不是正确的数据。
当前 Output
{ 类型:'缓冲区',数据:[67,40,3,10 ....] }
- 来自 api
....
return await workbook.csv.writeBuffer()
-反应方
const handleDownload = async() => {
const response= await userHelper.getExcel()
saveAs(new Blob([response?.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), 'deneme.csv');
}
您的响应类似于 ArrayBuffer,因此请将responseType
添加到配置中:
axios.get(
"http://localhost:5000/folder.zip", //hosted with server
{ responseType: 'arraybuffer' }))
.then(res => {
const file = new Blob([res], { type: 'text/csv' }
let url = URL.createObjectURL(file);
let a = document.createElement('a');
a.href = url;
a.download = 'deneme.csv';
a.click();
window.URL.revokeObjectURL(url);
});
希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.