Downloading files working

This commit is contained in:
Maxime Lalo 2023-09-26 15:48:14 +02:00
parent f45ce48ce3
commit c55a4ce014
2 changed files with 17 additions and 28 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable @next/next/no-img-element */
import React from "react";
import Typography, { ITypo, ITypoColor } from "../Typography";

View File

@ -34,7 +34,7 @@ type IState = {
selectedFile: File | null;
validatedPercentage: number;
document: Document | null;
fileData: string;
fileBlob: Blob | null;
};
class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
@ -49,7 +49,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
selectedFile: null,
validatedPercentage: this.getRandomPercentageForOcr(),
document: null,
fileData: "",
fileBlob: null,
};
this.closeModals = this.closeModals.bind(this);
@ -88,7 +88,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
)}
<div className={classes["file-container"]}>
<FilePreview
href={this.state.fileData}
href={this.state.fileBlob ? URL.createObjectURL(this.state.fileBlob) : ""}
fileName={this.state.selectedFile.file_name}
key={this.state.selectedFile.uid}
/>
@ -119,7 +119,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
<Button disabled>Télécharger</Button>
</>
)}
{this.state.document?.document_status === "VALIDATED" && (
{this.state.document?.document_status === "VALIDATED" && this.state.fileBlob && (
<Button onClick={this.downloadFile}>Télécharger</Button>
)}
</div>
@ -193,11 +193,10 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
}
private async getFilePreview(): Promise<void> {
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<IPropsClass, IState> {
}
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() {