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

Jitu picture Jitu · Jan 10, 2018 · Viewed 8.6k times · Source

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.

Answer

Ewomazino Ukah picture Ewomazino Ukah · Jan 10, 2018

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