Reputation: 523
I'm trying to download a file with my extension, it works alright with all browsers except safari. In safari instead of downloading the file, it just opens it in new tab with uri as url. How to download the file in safari with js code?
axios
.get(`http://localhost:3000/api/download`, {
params: { url },
responseType: "blob",
})
.then((response) => {
const disposition = response.headers["content-disposition"];
let filename = "test.mp3";
if (disposition && disposition.indexOf("attachment") !== -1) {
const matches = /filename="([^"]*)"/.exec(disposition);
if (matches != null && matches[1]) {
filename = matches[1];
}
}
const blob = response.data;
const reader = new FileReader();
reader.onloadend = () => {
const link = document.createElement("a");
link.href = reader.result;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
reader.readAsDataURL(blob);
})
.catch((error) => {
console.error("Download failed:", error);
})
.finally(() => {
spinner.classList.add("hidden");
downloadBtn.classList.remove("hidden");
});
Upvotes: 0
Views: 139