-1👍
The code you provided for handling the download using Axios looks correct. However, there might be an issue with the way you are handling the response or triggering the download. Here’s a modified version of your code that should work:
await axios
.get("download", {
responseType: "blob",
})
.then((response) => {
const downloadLink = document.createElement("a");
const url = URL.createObjectURL(new Blob([response.data]));
downloadLink.href = URL;
downloadLink.setAttribute("download", "test.zip");
// Programmatically trigger the download
downloadLink.click();
// Clean up the URL object after the download
URL.revokeObjectURL(url);
});
In this code, we create a URL object using URL.createObjectURL
and assign it to the href
property of the download link. We also set the download attribute to specify the filename. Finally, we trigger the download by programmatically clicking the download link.
Make sure you are using this code in an environment that supports the Blob and URL APIs, such as a web browser.
Source:stackexchange.com