From c55a4ce0148688cfd8d6e28f19f432d70fd1ce31 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 26 Sep 2023 15:48:14 +0200 Subject: [PATCH] :sparkles: Downloading files working --- .../DesignSystem/FilePreview/index.tsx | 1 + .../Layouts/Folder/ViewDocuments/index.tsx | 44 +++++++------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/front/Components/DesignSystem/FilePreview/index.tsx b/src/front/Components/DesignSystem/FilePreview/index.tsx index 1511d2a7..9e99150c 100644 --- a/src/front/Components/DesignSystem/FilePreview/index.tsx +++ b/src/front/Components/DesignSystem/FilePreview/index.tsx @@ -1,3 +1,4 @@ +/* eslint-disable @next/next/no-img-element */ import React from "react"; import Typography, { ITypo, ITypoColor } from "../Typography"; diff --git a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx index f371b6d6..caeb2c21 100644 --- a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx @@ -34,7 +34,7 @@ type IState = { selectedFile: File | null; validatedPercentage: number; document: Document | null; - fileData: string; + fileBlob: Blob | null; }; class ViewDocumentsClass extends BasePage { @@ -49,7 +49,7 @@ class ViewDocumentsClass extends BasePage { selectedFile: null, validatedPercentage: this.getRandomPercentageForOcr(), document: null, - fileData: "", + fileBlob: null, }; this.closeModals = this.closeModals.bind(this); @@ -88,7 +88,7 @@ class ViewDocumentsClass extends BasePage { )}
@@ -119,7 +119,7 @@ class ViewDocumentsClass extends BasePage { )} - {this.state.document?.document_status === "VALIDATED" && ( + {this.state.document?.document_status === "VALIDATED" && this.state.fileBlob && ( )}
@@ -193,11 +193,10 @@ class ViewDocumentsClass extends BasePage { } private async getFilePreview(): Promise { - const setState = this.setState; try { - const file: Blob = await Files.getInstance().download(this.state.selectedFile?.uid as string); + const fileBlob: Blob = await Files.getInstance().download(this.state.selectedFile?.uid as string); this.setState({ - fileData: URL.createObjectURL(file), + fileBlob, }); } catch (e) { console.log(e); @@ -205,27 +204,16 @@ class ViewDocumentsClass extends BasePage { } private downloadFile() { - const fileName = this.state.selectedFile?.file_path?.split("/").pop(); - fetch(Files.getInstance().getUploadLink(this.state.selectedFile?.uid as string)) - .then((resp) => resp.blob()) - .then((blob) => { - const url = window.URL.createObjectURL(blob); - const a = document.createElement("a"); - a.style.display = "none"; - a.href = url; - // the filename you want - a.download = fileName as string; - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - }) - .catch((e) => { - const a = document.createElement("a"); - a.href = this.state.selectedFile?.file_path as string; - a.target = "_blank"; - a.click(); - console.error(e); - }); + if (!this.state.fileBlob) return; + const url = window.URL.createObjectURL(this.state.fileBlob); + const a = document.createElement("a"); + a.style.display = "none"; + a.href = url; + // the filename you want + a.download = this.state.selectedFile?.file_name as string; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); } private getRandomPercentageForOcr() {