1

I return workbook as arraybuffer from api then trying to download on react side with blob.

It downloads file but csv file has bunch of numbers not the proper data.

Current Output

{ type: 'Buffer', data: [67,40,3,10....] }

-from 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');
  }
Fatih Can
  • 321
  • 4
  • 12

1 Answers1

0

Your response like ArrayBuffer, so please add responseType to config:

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

Hungnn
  • 68
  • 1
  • 2
  • 19