Download binary data as file
by marbx
JavaScript
const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays);
return blob;
}
const contentType = 'image/png';
const b64Data = 'QSBxdWljayBicm93biBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgZG9n';
const blob = b64toBlob(b64Data);
const blobUrl = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
//const linkSource = `base64,${b64Data}`;
downloadLink.href = blobUrl;
downloadLink.download = 'test_data.txt';
downloadLink.click();