Download file working (#33)

This commit is contained in:
Maxime Lalo 2023-05-09 17:04:17 +02:00 committed by GitHub
commit 8e6903b2f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,6 +66,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
this.hasPrevious = this.hasPrevious.bind(this);
this.hasNext = this.hasNext.bind(this);
this.downloadFile = this.downloadFile.bind(this);
this.refuseDocument = this.refuseDocument.bind(this);
}
@ -119,9 +120,7 @@ class ViewDocumentsClass extends BasePage<IPropsClass, IState> {
</>
)}
{this.state.document?.document_status === "VALIDATED" && (
<a href={this.state.selectedFile.file_path!} download target="_blank">
<Button>Télécharger</Button>
</a>
<Button onClick={this.downloadFile}>Télécharger</Button>
)}
</div>
</div>
@ -199,6 +198,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() {
// find diff
let difference = 100 - 90;