Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  106] [ 1]  / answers: 1 / hits: 20991  / 5 Years ago, wed, april 3, 2019, 12:00:00

I need to decode a base64 string into PDF file. Im using this code. But the window.atob command always report that error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.



I know that the file is correct, because I already decoded it using a website that decode base64 to pdf.
I dont know if it helps but we are using Aurelia Framework.



Function that convert



function converBase64toBlob(content, contentType) {
contentType = contentType || '';
var sliceSize = 512;
var byteCharacters = window.atob(content); //method which converts base64 to binary
var byteArrays = [
];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type: contentType
}); //statement which creates the blob
return blob;
}


Call of the function



self.blob = self.converBase64toBlob(result.contents[0].pdf.replace(/^[^,]+,/, ''), 'application/pdf');
self.blobURL = URL.createObjectURL(blob);
window.open(this.blobURL);

More From » aurelia

 Answers
23

I found the solution. The Api was returning the base64 string with a the character ''. So I removed all of then, and it works just fine.


[#52305] Friday, March 29, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ross

Total Points: 477
Total Questions: 97
Total Answers: 98

Location: France
Member since Thu, May 6, 2021
3 Years ago
;