I have been trying to figure out how can I create a download functionality when a user clicks on a button.
I tried to use the File save library but the result is not as expected, also I tried a turnaround and used a different approach but again the same.
When I download the file using the 2nd approach that is by using fetch API the file am getting is a corrupted file. Please see the code below with the codeSandbox link as well.
Approach 1
const onDownload1 = () => {
saveAs("../../testFile.docx", "testFile.docx");
};
Approach 2
const onDownload2 = () => {
fetch("../../testFile.docx").then((response) => {
response.blob().then((blob) => {
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = "testFile.docx";
a.click();
});
});
};
Full source code link
Thanks for the help and time.