when I download exel file with Axsio have problem but postman downloading correctly. why?

Viewed 267

When I trying download exel(.xlsx) file with axios have problem and when I open this file.

when open exel file show this and press 'Yes' after that open this error

I try everything and find one difference between those two file which I download axios and postman. Differences: axios file encoding is UTF-8, postman file encoding is ANSI.

It's my code:

export const DownloadMonthly = (from, to) => {
  return (dispatch) => {
      return getAxios()
        .get("/monthly/download", { params: {from, to}}, 
        {
          responseType: 'arraybuffer',
          headers: {
          'Content-Type': 'application/vnd.ms-excel'
        }})
        .then((response) => { 
          const url = window.URL.createObjectURL(new Blob([response.data], {type: 'application/vnd.ms-excel'}));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', "monthly.xlsx");
          document.body.appendChild(link);
          link.click();
        })
        .catch((err) => {
          console.error(err);
        }) 
  };
};
1 Answers

@Zuka Gaprindashvili i think the issue is the type that you are assigning to the file.

if you are downloading an .xlsx you will want application/vnd.openxmlformats-officedocument.spreadsheetml.sheet you content type is for .xls files

Related