输入不是有效的Base-64串,因为它包含一个非基64字 [英] The input is not a valid Base-64 string as it contains a non-base 64 character
问题描述
我有一个REST服务,读取文件并将其转换为字节数组,然后为Base64串后发送到另一个控制台应用程序。这部分发生正确的,但是当在收到申请相同的流,它被操纵,不再是一个有效的Base64编码字符串。有些垃圾字符越来越引入流。
这是我而流转换回字节获得唯一的例外是的输入不是一个有效的Base-64字符串,因为它包含非基本64个字符的,两个以上的填充字符,或非填充字符之间的空格字符
在提供服务:
[WebGet(UriTemplate =的ReadFile /转换,ResponseFormat = WebMessageFormat.Json)
公共字符串ExportToExcel()
{
字符串filetoexport =D:\\\\ SomeFile.xls
字节[]数据= File.ReadAllBytes(filetoexport);
变种S = Convert.ToBase64String(数据);
返回S;
}
在应用程序:
VAR的客户=新RESTClient实现(HTTP://本地主机:56877 /用户/);
VAR要求=新RestRequest(ReadFile的/转换,RestSharp.Method.GET);
request.AddHeader(接受,应用/ JSON的);
request.AddHeader(内容类型,应用/ JSON的);
request.OnBeforeDeserialization = RESP => {resp.ContentType =应用/ JSON的;};
VAR的结果= client.Execute(请求);
字节[] D = Convert.FromBase64String(result.Content);
很可能它变得转换为一个修改的Base64,其中 +
和 /
字符更改为 -
和 _
。见<一href=\"http://en.wikipedia.org/wiki/Base64#Implementations_and_history\">http://en.wikipedia.org/wiki/Base64#Implementations_and_history
如果是这样的话,你需要改回去:
字符串转换= base64String.Replace(' - ','+');
转换= converted.Replace('_','/');
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);
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('_', '/');
这篇关于输入不是有效的Base-64串,因为它包含一个非基64字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!