0
const data = ('start_date=2019-07-04&end_date=2019-07-24');
    axios({
      url: `http://localhost:4000/report/data?${data}`,
      method: 'GET',
      responseType: "arraybuffer", // important
    }).then(response => {
      // BLOB NAVIGATOR
      let blob = new Blob([response.data], { type: '' });

      if (navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob);
      } else {

        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.setAttribute('download', '');
        document.body.appendChild(link);
        link.download = [];
        link.click();
        document.body.removeChild(link);
      }
    });

I'm trying to download an xlsx file with reactJS but i'm receiving this message when i try to open my file after download:

"Excel can not open file 'file.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the file format"

Here's the frontend code:

Rishab
  • 1,484
  • 2
  • 11
  • 22
  • 1
    Have you tried looking at both files (with a text editor) and compare them? – Elias Jul 19 '19 at 13:48
  • 1
    I'm running into the same issue. One suggestion that may help is to set the filename in `link.setAttribute('download', '');` For example: `link.setAttribute('download', 'myFile.xlsx');` Also FWIW, if you're downloading an XLSX the Content-Type is: `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` from https://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document – tw1742 Jul 20 '19 at 17:16

0 Answers0