The input is not a valid Base-64 string as it contains a non-base 64 character

invalid character in a base64 string c#
invalid length for a base-64 char array or string.
not valid base64 string
base64 decode
system formatexception invalid character in a base-64 string
convert frombase64string formatexception
base64 characters
url safe base64

I have a REST service that reads a file and sends it to another console application after converting it to Byte array and then to Base64 string. This part happens correct but when the same stream is received at the application, it gets manipulated and is no more a valid Base64 string. Some junk characters are getting introduced into the stream.

The exception that i get while converting the stream back to Byte is "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters".

At Service:

[WebGet(UriTemplate = "ReadFile/Convert", ResponseFormat = WebMessageFormat.Json)]  
public string ExportToExcel()
  {
      string filetoexport = "D:\\SomeFile.xls";
      byte[] data = File.ReadAllBytes(filetoexport);
      var s = Convert.ToBase64String(data);
      return s;
  }

At Application:

       var client = new RestClient("http://localhost:56877/User/");
       var request = new RestRequest("ReadFile/Convert", RestSharp.Method.GET);
       request.AddHeader("Accept", "application/Json");
       request.AddHeader("Content-Type", "application/Json");
       request.OnBeforeDeserialization = resp => {resp.ContentType =    "application/Json";};
       var result = client.Execute(request);
       byte[] d = Convert.FromBase64String(result.Content); 

Check if your image data contains some header information at the beginning:

imageCode = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkC...

This will cause the above error.

Just remove everything in front of and including the first comma, and you good to go.

imageCode = "iVBORw0KGgoAAAANSUhEUgAAAMgAAABkC...

Convert.FromBase64String does not work in code but works in , FromBase64String(str) : System.FormatException: The input is not a valid Base-​64 string as it contains a non-base 64 character, more than two  Not ok! The first line does not work, as FromBase64String returns byte[], not String. You want this: string base64String = Textbox1.Text.Split(',')[1];. It is unneccessary to perform ToString on the Text property of a TextBox - it is already a String. Also you should really make sure that there actually is a comma in Textbox1.Text

Very possibly it's getting converted to a modified Base64, where the + and / characters are changed to - and _. See http://en.wikipedia.org/wiki/Base64#Implementations_and_history

If that's the case, you need to change it back:

string converted = base64String.Replace('-', '+');
converted = converted.Replace('_', '/');

Convert.FromBase64String different behavior in .NET Core 2.1 vs , FormatException: 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  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.

We can remove unnecessary string input in front of the value.

string convert = hdnImage.Replace("data:image/png;base64,", String.Empty);

byte[] image64 = Convert.FromBase64String(convert);

Exception: The input is not a valid Base-64 string as it contains a , The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character  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.

Just in case you don't know the type of uploaded image, and you just you need to remove its base64 header:

 var imageParts = model.ImageAsString.Split(',').ToList<string>();
 //Exclude the header from base64 by taking second element in List.
 byte[] Image = Convert.FromBase64String(imageParts[1]);

Error while decoding. The input is not a valid base-64 string as it , Exception: 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  Input is not valid as base 64 string as it contains non base 64 Character Suggested Answer If you have not tried restarting the NAV service yet then Please restart the NAV service related to this database and check if it resolves the issue.

Since you're returning a string as JSON, that string will include the opening and closing quotes in the raw response. So your response should probably look like:

"abc123XYZ=="

or whatever...You can try confirming this with Fiddler.

My guess is that the result.Content is the raw string, including the quotes. If that's the case, then result.Content will need to be deserialized before you can use it.

Convert.FromBase64String(String) Method (System), Quote: Error while decoding. The input is not a valid base-64 string as it contains a non-base 64 character, more than two padding characters. The input is not a valid base-64 string as it contains a non-base 64 character, more than two padding characters The message tell you all, you get the error message because your input is not base64 encoded. Just compare encoded data with decoded, if you look carefully, some parts are encoded and others are not.

Base 64 Issue | C# Online Compiler, The length of s , ignoring white-space characters, is not zero or a multiple of 4. -or​-. The format of s is invalid. s contains a non-base-64 character, more than two  The exception that i get while converting the stream back to Byte is “The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters”.

Base64 decoding bug that is present in all version of .NET, FormatException: 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 input is not a valid Base64 string, as it contains a character that is not a base 64 character, more than two fill characters, or an invalid character among the fill characters. Can anyone please help me :)

This means every 4 characters in the Base64 encoded string represent 3 The input is not a valid Base-64 string as it contains a non-base 64  he 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. in MVC4.

Comments
  • Probably this has to do with Encoding.
  • Do you know what "junk characters" are being inserted?
  • The updated code is helpful. Now we need to see the string that you send (i.e. s on the service) and the content that's received (i.e. result.content. You don't need to post the entire string, just up to the first mangled character (or, if that's still too long, some substrings that show what was sent and what was received).
  • @JimMischel yeah, i noticed that '/' is getting replaced with '\/'
  • @RohitVerma For the slash getting replaced, is that in the raw HTML contents (Fiddler will tell you), or in result.Content? That will tell you if the problem is with the server or the client.
  • Had this exact issue somehow. Logic is to remove everything after , if data: is present. Bam. Working now.
  • var cleanerBase64 = imageCode.Substring(22); // remove data:image/png;base64
  • Just a little bit of code to help...... if (this.imageCode.Contains(',')) this.imageCode = this.imageCode.Substring(this.imageCode.IndexOf(",") + 1, this.imageCode.Length - (this.imageCode.IndexOf(",") + 1));
  • I would use: .Split(',')[1]
  • str.Substring(str.LastIndexOf(',') + 1) should do it.