From fb894799bea01f5ab70ddee4cd143caec92f63a7 Mon Sep 17 00:00:00 2001 From: gissehel Date: Tue, 24 Dec 2024 11:28:17 +0100 Subject: [PATCH] Add downloadUrl snippet --- snippet/downloadUrl.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippet/downloadUrl.js diff --git a/snippet/downloadUrl.js b/snippet/downloadUrl.js new file mode 100644 index 0000000..80d05ad --- /dev/null +++ b/snippet/downloadUrl.js @@ -0,0 +1,19 @@ +/** + * + * @param {String} url The url to download + * @param {String} filename The filename to use to save the file + * @returns + */ +const downloadUrl = async (url, filename) => { + const response = await fetch(url); + const blob = await response.blob(); + const urlContent = window.URL.createObjectURL(blob); + const a = createElementExtended('a', { + attributes: { + href: urlContent, + target: '_blank', + download: filename, + }, + }) + a.click() +} \ No newline at end of file