My base64 String appears as
"UklGRkAdEQBXQVZFZm10IBIAAAABAAEAg..."
.
I am attempting to download this file using my browser. Therefore,
- I convert it to a blob with the following function:
b65toBlob: function(b64Data, sliceSize = 512) {
let byteCharacters = atob(b64Data);
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
let byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
let blob = new Blob(byteArrays);
return blob;
}
- Generate a URL from the blob using
URL.createObjectURL(this.b64toBlob(base64))
- Simulate a click on an
<a>
tag with the created URL.
While Mozilla Firefox can correctly identify the file type and prompt the user to download it with the correct extension, Chrome seems to suggest downloading it as a txt file regardless of the actual file type, posing an issue.
I could specify the file type while creating the blob file to ensure that Chrome behaves properly, but I am unsure how to determine the correct MIME type from the given base64 string.