Browse Source

🌟 Added download functionality

🔄 Updated downloadPhoto function
🐞 Fixed file naming issue
📦 Refactored code for clarity
🚧 Work in progress: Download feature
📝 Documented downloadPhoto function
🎉 Completed file download implementation
🧰 Improved error handling
 Tested and verified download functionality
🔧 Fine-tuned downloadPhoto behavior
main
John Doe 1 year ago
parent
commit
b7cc166c7e
  1. 26
      src/components/listing-image-gallery/utils/downloadPhoto.ts

26
src/components/listing-image-gallery/utils/downloadPhoto.ts

@ -0,0 +1,26 @@
function forceDownload(blobUrl: string, filename: string) {
let a: any = document.createElement("a");
a.download = filename;
a.href = blobUrl;
document.body.appendChild(a);
a.click();
a.remove();
}
export default function downloadPhoto(url: string, filename: string) {
if (!filename) {
filename = url.split("\\").pop()?.split("/").pop() || "";
}
fetch(url, {
headers: new Headers({
Origin: location.origin,
}),
mode: "cors",
})
.then((response) => response.blob())
.then((blob) => {
let blobUrl = window.URL.createObjectURL(blob);
forceDownload(blobUrl, filename);
})
.catch((e) => console.error(e));
}
Loading…
Cancel
Save