How download Excel file in Vue.js application correctly? How download Excel file in Vue.js application correctly? vue.js vue.js

How download Excel file in Vue.js application correctly?


You need to add the response type as a third argument in your post call

{    responseType: 'blob'}

Your final code like that

axios.post(config.backendHost + '/excel', {    file_name: fileName}, {    responseType: 'blob'}).then((response) => {    const url = URL.createObjectURL(new Blob([response.data], {        type: 'application/vnd.ms-excel'    }))    const link = document.createElement('a')    link.href = url    link.setAttribute('download', fileName)    document.body.appendChild(link)    link.click()});

Or you can use the library FileSaver.js to save your file

import FileSaver from 'file-saver'axios.post(config.backendHost + '/excel', {    file_name: fileName}, {    responseType: 'blob'}).then((response) => {    // response.data is a blob type    FileSaver.saveAs(response.data, fileName);});


my case worked:

  axios.get(`/api/v1/companies/${companyId}/export`, {    responseType: 'blob',  }).then((response) => {    const url = URL.createObjectURL(new Blob([response.data]))    const link = document.createElement('a')    link.href = url    link.setAttribute(      'download',      `${companyId}-${new Date().toLocaleDateString()}.xlsx`    )    document.body.appendChild(link)    link.click()  })