Downloading excel(.xlsx) with blob and FileSaver.js in vue project getting coruppted

10,330

try this

     return Vue.axios.get(`api/excel_sheet`, {
        responseType: 'blob',
      }).then(response => {
      FileSaver.saveAs(response.data, 'Export2.xlsx')
};

u need to specify the response type as blob

Share:
10,330
Jitu
Author by

Jitu

Updated on June 23, 2022

Comments

  • Jitu
    Jitu almost 2 years

    I am trying to download file by sending an Api request in my Vue project, and with response data I am trying to save the file using blob and FileSaver.js

    sendAjaxCall.then(response => {
          let file = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
          FileSaver.saveAs(file, 'Export2.xlsx')
    }
    

    But I can not open the file, it is getting corrupted.

  • Jitu
    Jitu over 6 years
    Thank you Mazino, Actully I was missing the responseType, your solution working. and also I have soloved it by sending the responseType = 'arraybuffer' and blob the response and then saved it.
  • Ajithkumar S
    Ajithkumar S almost 5 years
    Thank you @MazinoSUkah.. It worked like a charm.. :)