javascriptaurelia

Error using 'atob' command - Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded


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);

Solution

  • 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.