1

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?

1
  • try with the *.png image. May be your server can accept only base64/png Commented Sep 11, 2015 at 20:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

I experienced the same issue and founds out that my complete dataurl contained not only padding characters at the end, but also padding characters in the middle of the dataurl. I used the following code to fix the base64string (but it still has a bug):

private static string getCleanedBase64String(string base64String)
    {
        string tempString = base64String.Replace("_", "/").Replace("-", "+").Replace("=", "");
        return tempString + new string('=', (4 - tempString.Length % 4) % 4);
    }

Comments

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.