I am trying to download a docx file from server using axios. The downloaded file is larger than the original file and appears corrupt, not able to open. The issue is not with the servlet used to download because if I use the link directly in a browser it gets downloaded perfectly.
I came across this link and tried that as well but in vain. Please let me know what I might be doing wrong. Appreciate your help. Download binary file with Axios
axios({
method: "GET",
url: "https://example.com/apps/GetFile?file=test.docx", //
responsetype: "blob",
config: {
headers: {
'Access-Control-Allow-Origin': 'http://localhost:1337',
'Content-Type': 'application/json'
}
}
}).then(function(response) {
const downloadUrl = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', 'test.docx');
document.body.appendChild(link);
link.click();
})
.catch(function(error) {
alert(error);
});