admin管理员组文章数量:1291320
I need to decode a base64 string into PDF file. Im using this code. But the window.atob mand 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);
I need to decode a base64 string into PDF file. Im using this code. But the window.atob mand 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);
Share
Improve this question
asked Apr 3, 2019 at 17:53
Philippe Philippe
731 gold badge1 silver badge6 bronze badges
3
- note entirely sure but Ive seen someone add '=' to the end up to 2 times and then it worked – jonathan Heindl Commented Apr 3, 2019 at 17:55
-
I dont think you can just remove chars from the base64
.replace(/^[^,]+,/, '')
without breaking the encoding – jonathan Heindl Commented Apr 3, 2019 at 17:56 - Thanks for the help, I already tried without the replace() and didn't work. About the adding '=' at the end, did not worked also. – Philippe Commented Apr 3, 2019 at 18:15
2 Answers
Reset to default 5I 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.
Make sure you handle the undefined
case properly. In my case, my token was being nulled out after refresh, so I was essentially doing atob()
on an undefined
token, causing this error.
本文标签:
版权声明:本文标题:javascript - Error using 'atob' command - Failed to execute 'atob' on 'Window': 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530604a2383734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论