0

After converting a pdf file using Cordova Plugin FileReader and send it to a .Net server, the server throws this error "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." on converting the base64 string to byte array.

Java Script Code:

window.resolveLocalFileSystemURL(data, 
function (fileEntry) {
    fileEntry.file(function (fileObj) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {             
            base64StringDocument = evt.target.result;
        };
        reader.readAsDataURL(fileObj);    
    },
    function (error) {
        console.log('get fileEntry error: ' + error.message);
    });
},
function (error) {
  console.log('resolve error: ' + error.message);
});

C# Code:

int startIndexOfBase64 = base64String.IndexOf("base64,") + "base64,".Length;
base64String = base64String.Substring(startIndexOfBase64);

byte[] blob = Convert.FromBase64String(base64String);

Base64 string start:

data:application/pdf;base64,JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UKL1BhcmVudCAyIDAgUgovUmVzb3VyY2VzIDQgMCBSCi9NZWRpYUJveCBbMCAwIDU5NSA4NDJdCi9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKNCAwIG9iago8PCAvUHJvY1NldCBbL1BERiAvSW

Base64 string end:

wMDIyOTI1NiAwMDAwMCBuDQowMDAwMjI5MzYxIDAwMDAwIG4NCjAwMDAyMjk0MzMgMDAwMDAgbg0KMDAwMDIyOTU5MiAwMDAwMCBuDQowMDAwNDU1MDkwIDAwMDAwIG4NCnRyYWlsZXIKPDwgL1NpemUgMTMKL1Jvb3QgMSAwIFIKPj4Kc3RhcnR4cmVmCjQ1NTE3NwolJUVPRg==

2
  • I don't see how you send the data to the server. Are you sending it as a query in an url, like example.com?data=data:application/pdf;base64,JVBERi0xLjQKMSAwIG9...? In that case your plus signs in the base64-data might be replaced by space while decoding in on the server side. Commented Jan 19, 2017 at 17:19
  • Things I would do: Check the length of your base64 string after you've stripped stuff off the start. It should be a multiple of 4 (I believe you'd get a different error if this was wrong but its easy enough to check). Then I'd just create a loop that checks each character of your base64 string looking for anything that isn't a-zA-Z0-9+/ . Note that if you find an = before the end that too is bad. The error seems pretty clear though, you just need to find where the error is and then deal with it. Once you have found the rogue characters then we might be able to help work out why they are there. Commented Jan 19, 2017 at 17:21

1 Answer 1

1

Thanks alot for the comments, but applying the below regular expression over the base64 solved the issue.

window.resolveLocalFileSystemURL(data, 
function (fileEntry) {
    fileEntry.file(function (fileObj) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {            
            base64StringDocument = evt.target.result.match(/,(.*)$/)[1];
        };
        reader.readAsDataURL(fileObj);    
    },
    function (error) {
        console.log('get fileEntry error: ' + error.message);
    });
},
function (error) {
  console.log('resolve error: ' + error.message);
});
Sign up to request clarification or add additional context in comments.

1 Comment

It's good that you found your own answer, though it looks like you're using regex to split the string on a single, unique character. It's less expensive to just use evt.target.result.split(',')[1]. It's probably not a big deal in this case, but for something that's explicitly delimited, there's little reason to bring regex in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.