✨ Downloading files working
This commit is contained in:
parent
f45ce48ce3
commit
c55a4ce014
@ -1,3 +1,4 @@
|
||||
/* eslint-disable @next/next/no-img-element */
|
||||
import React from "react";
|
||||
|
||||
import Typography, { ITypo, ITypoColor } from "../Typography";
|
||||
|
@ -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);
|
||||
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 = fileName as string;
|
||||
a.download = this.state.selectedFile?.file_name 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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user