输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符 [英] The input is not a valid Base-64 string as it contains a non-base 64 character

查看:41
本文介绍了输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST 服务,它读取一个文件,然后将它转换为 Byte 数组,然后转换为 Base64 字符串,然后将其发送到另一个控制台应用程序.这部分有效,但是当应用程序接收到相同的流时,它会被操纵并且不再是有效的 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 works, but when the same stream is received at the application, it gets manipulated and is no longer a valid Base64 string. Some junk characters are getting introduced into the stream.

将流转回Byte时收到的异常是

The exception received when converting the stream back to Byte is

输入不是有效的 Base-64 字符串,因为它包含非 base-64字符、两个以上的填充字符或非空格填充字符之间的字符

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

服务:

[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;
  }

在申请时:

       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...

这会导致上面的错误.

只需删除第一个逗号前面的所有内容,包括第一个逗号,您就可以开始了.

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

imageCode = "iVBORw0KGgoAAAANSUhEUgAAAMgAAABkC...

这篇关于输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆