I've created a function that takes a blob
and fileName
which is supposed to download that blob implemented as follows:
const blobToBase64 = (blob, callback) => {
const reader = new FileReader();
reader.onloadend = () => {
const base64 = reader.result;
console.log({ base64 });
callback(base64);
};
reader.readAsDataURL(blob);
};
const downloadFile = (blob, fileName) => () => {
const link = document.createElement('a');
blobToBase64(blob, (base64) => {
link.href = base64;
link.download = fileName;
link.click();
});
};
downloadFile(myBlob, myFileName);
To try debug this I've made a console.log
to log out the value of base64
which is created by reader.result
.
That base64
value is data:application/octet-stream;base64,Mzc4MDY4...
My PDF file get's downloaded but it's corrupted. What am I doing wrong in my file download implementation?
Let me know if there are any additional details that might help with this? I'm 100% sure that the blob itself is not a corrupted file.
application/pdf
, notapplication/octet-stream
– Andriaapplication/octet-stream
withapplication/pdf
doesn't fix it. – Ahwaz