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

I have a REST service that reads a file and sends it to another console application after converting it to an array of bytes and then to a Base64 string. This part is executed correctly, but when the same stream is received in the application, it is processed and is no longer a valid Base64 string. Some unwanted characters are injected into the stream.

The exception that I get when converting the stream back to bytes is . The input is not a valid Base-64 string because it contains a non-base 64 character, more than two padding characters or a non-space character among padding characters . "

In the 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; } 

In the 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); 
+58
c # file-io base64
Feb 27 '13 at 2:04 on
source share
8 answers

Perhaps it is converted to a modified Base64, where the characters + and / changed to - and _ . See http://en.wikipedia.org/wiki/Base64#Implementations_and_history

If in this case you need to change it:

 string converted = base64String.Replace('-', '+'); converted = converted.Replace('_', '/'); 
+50
Feb 27 '13 at 14:09
source share

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

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

This will result in the above error.

Just delete everything before the first comma and turn it on and you will go well.

 imageCode = "iVBORw0KGgoAAAANSUhEUgAAAMgAAABkC... 
+49
May 29 '15 at 9:22
source share

We can remove unnecessary line input before the value.

 string convert = hdnImage.Replace("data:image/png;base64,", String.Empty); byte[] image64 = Convert.FromBase64String(convert); 
+18
Mar 29 '16 at 9:40
source share

I arranged a similar context as you described, and I came across the same error. I managed to get it to work by removing " from the beginning and end of the content and replacing \/ with / .

Here is the code snippet:

 var result = client.Execute(request); var response = result.Content .Substring(1, result.Content.Length - 2) .Replace(@"\/","/"); byte[] d = Convert.FromBase64String(response); 

Alternatively, you can use XML for the response format:

 [WebGet(UriTemplate = "ReadFile/Convert", ResponseFormat = WebMessageFormat.Xml)] public string ExportToExcel() { //... } 

On the client side:

 request.AddHeader("Accept", "application/xml"); request.AddHeader("Content-Type", "application/xml"); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/xml"; }; var result = client.Execute(request); var doc = new System.Xml.XmlDocument(); doc.LoadXml(result.Content); var xml = doc.InnerText; byte[] d = Convert.FromBase64String(xml); 
+3
Feb 27 '13 at 15:45
source share

Since you are returning the string as JSON, this string will include quotes for opening and closing in the original response. So your answer should look like this:

 "abc123XYZ==" 

or something else ... You can try to confirm this with Fiddler.

I assume that result.Content is the source string, including quotation marks. If this is the case, then result.Content will need to be deserialized before you can use it.

+2
Feb 27 '13 at 14:42
source share

Just in case, you don’t know the type of image loaded, and you just 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]); 
+1
Aug 23 '17 at 13:37 on
source share

As noted by Alex Filipovich, the problem was in the wrong encoding. The file I read was UTF-8-BOM and threw the above error on Convert.FromBase64String() . Switching to UTF-8 worked without problems.

0
Aug 03 '18 at 8:16
source share

And sometimes it started with double quotes, in most cases when you call the API from dotNetCore 2

 string string64 = string64.Replace(@"""", string.Empty); byte[] bytes = Convert.ToBase64String(string64); 
0
03 Feb '19 at 10:56
source share



All Articles