Reputation: 147
hello I am trying to create a download with axios for an excel file and I was not able to download and open it, could you please check with me what is the problem??
const blob = new Blob([result.data], { type: 'application/vnd.ms-excel' });
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', 'atetes.xlsx')
document.body.appendChild(link)
link.click()
I tried many solutions but i always get a corrupted file
Upvotes: 7
Views: 3191
Reputation: 2252
I was also facing the same issue and spend enough time on it, The reason as you will notice that the response object contains data which is invalid so parsing on frontend got wrong so, in my case, the solution was pretty simple. While making an API call I added responseType as array buffer to make it work:
return await client({ method: "get", url: `[YOUR API ENDPOINT]`,
responseType: "arraybuffer",
headers: {
"Authorization": token,
"Accept": "application/octet-stream",
}});
Upvotes: 5