From b7cc166c7eb1539ce596fea4a105890eba4c2a36 Mon Sep 17 00:00:00 2001 From: John Doe Date: Mon, 11 Sep 2023 17:32:01 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=9F=20Added=20download=20functionality?= =?UTF-8?q?=20=F0=9F=94=84=20Updated=20downloadPhoto=20function=20?= =?UTF-8?q?=F0=9F=90=9E=20Fixed=20file=20naming=20issue=20=F0=9F=93=A6=20R?= =?UTF-8?q?efactored=20code=20for=20clarity=20=F0=9F=9A=A7=20Work=20in=20p?= =?UTF-8?q?rogress:=20Download=20feature=20=F0=9F=93=9D=20Documented=20dow?= =?UTF-8?q?nloadPhoto=20function=20=F0=9F=8E=89=20Completed=20file=20downl?= =?UTF-8?q?oad=20implementation=20=F0=9F=A7=B0=20Improved=20error=20handli?= =?UTF-8?q?ng=20=E2=9C=85=20Tested=20and=20verified=20download=20functiona?= =?UTF-8?q?lity=20=F0=9F=94=A7=20Fine-tuned=20downloadPhoto=20behavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/downloadPhoto.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/components/listing-image-gallery/utils/downloadPhoto.ts diff --git a/src/components/listing-image-gallery/utils/downloadPhoto.ts b/src/components/listing-image-gallery/utils/downloadPhoto.ts new file mode 100644 index 0000000..bc43815 --- /dev/null +++ b/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)); +}