L. Desjardins L. Desjardins - 2 months ago 37
Javascript Question

FileReader.readAsDataURL result returning format exception when entered in Convert.FromBase64String

I'm using the following to convert an image to a base64 encoded string. On the client website in javascript:

var reader = new FileReader();
reader.onloadend = function () {
data64 = reader.result;
};

reader.readAsDataURL(myFile);


Now I pass this data to the server, which does the following:

var data = Convert.FromBase64String(data64);


However this results in a format exception:


The format of s is invalid. s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.


My input file is one of the sample images found on Windows 7 -> My Pictures/Sample Pictures/Tulips.jpeg

How can I attempt to debug what is causing the problem for such a large result output?

Answer

Okay, I have worked around this by using reader.readAsBinaryString instead and then converting this using btoa.

This seems to be accepted fine in Convert.FromBase64String