Downloading file working

This commit is contained in:
Maxime Lalo 2023-05-09 15:33:42 +02:00
parent ad874c6c93
commit 8a524348ec

View File

@ -1,5 +1,3 @@
import "reflect-metadata";
import LeftArrowIcon from "@Assets/Icons/left-arrow.svg"; import LeftArrowIcon from "@Assets/Icons/left-arrow.svg";
import RightArrowIcon from "@Assets/Icons/right-arrow.svg"; import RightArrowIcon from "@Assets/Icons/right-arrow.svg";
import Documents from "@Front/Api/LeCoffreApi/SuperAdmin/Documents/Documents"; import Documents from "@Front/Api/LeCoffreApi/SuperAdmin/Documents/Documents";
@ -22,6 +20,7 @@ import BasePage from "../../Base";
import classes from "./classes.module.scss"; import classes from "./classes.module.scss";
import OcrResult from "./OcrResult"; import OcrResult from "./OcrResult";
import "reflect-metadata";
type IProps = {}; type IProps = {};
type IPropsClass = { type IPropsClass = {
@ -66,6 +65,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
this.hasPrevious = this.hasPrevious.bind(this); this.hasPrevious = this.hasPrevious.bind(this);
this.hasNext = this.hasNext.bind(this); this.hasNext = this.hasNext.bind(this);
this.downloadFile = this.downloadFile.bind(this);
} }
public override render(): JSX.Element | null { public override render(): JSX.Element | null {
@ -118,9 +118,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
</> </>
)} )}
{this.state.document?.document_status === "VALIDATED" && ( {this.state.document?.document_status === "VALIDATED" && (
<a href={this.state.selectedFile.file_path!} download target="_blank"> <Button onClick={this.downloadFile}>Télécharger</Button>
<Button>Télécharger</Button>
</a>
)} )}
</div> </div>
</div> </div>
@ -198,6 +196,30 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
} }
} }
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() { private getRandomPercentageForOcr() {
// find diff // find diff
let difference = 100 - 90; let difference = 100 - 90;