简体   繁体   English

无法从 api 下载 excel 文件 - xlsx/csv

[英]Can't download excel file from api - xlsx/csv

I return workbook as arraybuffer from api then trying to download on react side with blob.我从 api 将工作簿作为 arraybuffer 返回,然后尝试使用 blob 在反应端下载。

It downloads file but csv file has bunch of numbers not the proper data.它下载文件,但 csv 文件有一堆数字不是正确的数据。

Current Output当前 Output

{ type: 'Buffer', data: [67,40,3,10....] } { 类型:'缓冲区',数据:[67,40,3,10 ....] }

-from api - 来自 api

  ....
 return await workbook.csv.writeBuffer()

-react side -反应方

const handleDownload = async() => {
    const response= await userHelper.getExcel()

    saveAs(new Blob([response?.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), 'deneme.csv');
  }

Your response like ArrayBuffer, so please add responseType to config:您的响应类似于 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);
});

Hope that it helps希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM