From 8a524348ec7c696f9bbc489ca99b6114b82f405d Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 9 May 2023 15:33:42 +0200 Subject: [PATCH 1/2] :sparkles: Downloading file working --- .../Layouts/Folder/ViewDocuments/index.tsx | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx index 9d0ec40a..4c0f32df 100644 --- a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx @@ -1,5 +1,3 @@ -import "reflect-metadata"; - import LeftArrowIcon from "@Assets/Icons/left-arrow.svg"; import RightArrowIcon from "@Assets/Icons/right-arrow.svg"; import Documents from "@Front/Api/LeCoffreApi/SuperAdmin/Documents/Documents"; @@ -22,6 +20,7 @@ import BasePage from "../../Base"; import classes from "./classes.module.scss"; import OcrResult from "./OcrResult"; +import "reflect-metadata"; type IProps = {}; type IPropsClass = { @@ -66,6 +65,7 @@ class ViewDocumentsClass extends BasePage { this.hasPrevious = this.hasPrevious.bind(this); this.hasNext = this.hasNext.bind(this); + this.downloadFile = this.downloadFile.bind(this); } public override render(): JSX.Element | null { @@ -118,9 +118,7 @@ class ViewDocumentsClass extends BasePage { )} {this.state.document?.document_status === "VALIDATED" && ( - - - + )} @@ -198,6 +196,30 @@ class ViewDocumentsClass extends BasePage { } } + private downloadFile() { + const fileName = this.state.selectedFile?.file_path?.split("/").pop(); + fetch(this.state.selectedFile?.file_path 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); + }); + } + private getRandomPercentageForOcr() { // find diff let difference = 100 - 90; From 244993d05daafd7508415eb77fbf2b0ed839bdbf Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 9 May 2023 16:26:31 +0200 Subject: [PATCH 2/2] :bug: Reflect metadata wrong place --- src/front/Components/Layouts/Folder/ViewDocuments/index.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx index 4c0f32df..311a0303 100644 --- a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx @@ -1,3 +1,5 @@ +import "reflect-metadata"; + import LeftArrowIcon from "@Assets/Icons/left-arrow.svg"; import RightArrowIcon from "@Assets/Icons/right-arrow.svg"; import Documents from "@Front/Api/LeCoffreApi/SuperAdmin/Documents/Documents"; @@ -20,7 +22,6 @@ import BasePage from "../../Base"; import classes from "./classes.module.scss"; import OcrResult from "./OcrResult"; -import "reflect-metadata"; type IProps = {}; type IPropsClass = {