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
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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.

本文标签: