Reputation: 11
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==
Upvotes: 0
Views: 2327
Reputation: 11
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);
});
Upvotes: 1