From e3c31533a940d77b6e4ba31992de15c1acbae8a4 Mon Sep 17 00:00:00 2001
From: Liliana Sanfilippo <liliana.sanfilippo@uni-bielefeld.de>
Date: Sat, 28 Sep 2024 19:27:47 +0200
Subject: [PATCH] DownloadImageButton

---
 src/components/Buttons.tsx | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/components/Buttons.tsx b/src/components/Buttons.tsx
index 32105fad..c0d731fd 100644
--- a/src/components/Buttons.tsx
+++ b/src/components/Buttons.tsx
@@ -72,17 +72,40 @@ const handleDownload = () => {
         console.error("Error fetching the file:", error);
     });
 };
+};
+
+export const DownloadImageButton = ({ url, fileName, children}: {url: string, fileName: string, children: React.ReactNode}) => {
+  const handleDownload = () => {
+      fetch(url)
+      .then((response) => response.blob())
+      .then((blob) => {
+          const url = window.URL.createObjectURL(new Blob([blob]));
+          const link = document.createElement("a");
+          link.href = url;
+          link.download = fileName || "downloaded-file";
+          document.body.appendChild(link);
+  
+          link.click();
+  
+          document.body.removeChild(link);
+          window.URL.revokeObjectURL(url);
+      })
+      .catch((error) => {
+          console.error("Error fetching the file:", error);
+      });
+  };
   
 return (
 
     <a type="button" onClick={handleDownload} className="download-butt">
-        Download
+        {children}
     </a>
 
 );
 };
 
 
+
 export function TabButtonRow({data, classy, opentype, closing}: {data: Array<TabDatensatz>, classy?: string, opentype: string, closing: string}){
   let rows = []
   for ( let i = 0;  i < data.length; i++ ){
-- 
GitLab