In react, I am testing my file download based on John Culviner's solution mentioned in this post
axios.post('api/downloadMyFile',
data
).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]))
const a = document.createElement('a');
a.href = url;
a.download = "test.zip"
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}).catch((err) => {
}
So file test.zip
is getting downloaded. but when I tried to open it after saving it I am getting Compressed Zip folder error in windows.
Also, I noticed that I don't need to specify the name of the file in the line a.download = "test.zip"
because the webservice is getting the file from shared storage and it already has a name.
So in this case, do I need to have the filename also in the response
object? something like response.filename
so that I could use it in the following line instead of naming it manually:
a.download = response.filename