From c512c97521796dec30c698b43ce3f9708c2fb089 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Tue, 11 Oct 2022 23:47:56 +0200 Subject: [PATCH] added `download` util --- packages/app/src/utils/download/index.js | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/app/src/utils/download/index.js diff --git a/packages/app/src/utils/download/index.js b/packages/app/src/utils/download/index.js new file mode 100644 index 00000000..33f7de85 --- /dev/null +++ b/packages/app/src/utils/download/index.js @@ -0,0 +1,32 @@ +export default (uri, filename) => { + fetch(uri, { + method: "GET", + headers: { + "Content-Type": "application/pdf", + }, + }) + .then((response) => response.blob()) + .then((blob) => { + if (!filename) { + filename = uri.split("/").pop() + } + + // Create blob link to download + const url = window.URL.createObjectURL(new Blob([blob])) + + const link = document.createElement("a") + + link.href = url + + link.setAttribute("download", filename) + + // Append to html link element page + document.body.appendChild(link) + + // Start download + link.click() + + // Clean up and remove the link + link.parentNode.removeChild(link) + }) +} \ No newline at end of file