System.FormatException:Base-64 char数组或字符串的长度无效。 [英] System.FormatException: Invalid length for a Base-64 char array or string.

查看:76
本文介绍了System.FormatException:Base-64 char数组或字符串的长度无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个代码来上传pdf文档。运行代码时会出错 -

\ System.FormatException:Base的长度无效-64 char数组或字符串。



我的代码:

  byte  [] bDoccontent =  null ; 
HttpWebRequest hwrProg =(HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse hwrpProg =(HttpWebResponse)hwrProg.GetResponse();
StreamReader sr = new StreamReader(hwrpProg.GetResponseStream(),Encoding.ASCII);
bDoccontent = Convert.FromBase64String(sr.ReadToEnd());
sr.Close();
Response.Clear();
Response.ContentType = Application / pdf;
Page.Response.AddHeader( Content-Disposition inline; filename = wei.pdf);
Response.AddHeader( content-length,bDoccontent.Length.ToString()) ;
Response.BinaryWrite(bDoccontent);
Response.Flush();
Response.Close();



请帮帮我:(

解决方案

错误意味着ReadToEnd方法返回的字符串不是Base64字符串...

http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v = vs.110).aspx [ ^ ]


您需要准确查看Web请求返回的内容:它不仅仅是一个Base64字符串,可能还有一些包装。

使用调试器,或将原始数据记录到文件并检查它:我认为你需要删除的东西 - 但我们不能为你做,因为我们无法访问你的代码或URL ......

您应该从流中读取一系列字符,然后再读到最终字符在进行转换之前,请先检查您的流阅读器。

  byte  [] bDoccontent = ; 
HttpWebRequest hwrProg =(HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse hwrpProg =(HttpWebResponse)hwrProg.GetResponse();
StreamReader sr = new StreamReader(hwrpProg.GetResponseStream(),Encoding.ASCII);
char [] base64CharArray = new char [sr.BaseStream.Length]; //
sr.Read( base64CharArray, 0 ,( int )sr.BaseStream.Length); //
sr.Close(); //
string base64String = new string (base64CharArray); //
bDoccontent = Convert.FromBase64String(base64String); //
Response.Clear();
Response.ContentType = Application / pdf;
Page.Response.AddHeader( Content-Disposition inline; filename = wei.pdf);
Response.AddHeader( content-length,bDoccontent.Length.ToString()) ;
Response.BinaryWrite(bDoccontent);
Response.Flush();
Response.Close();


Hi,
I have a code to upload the pdf document.While running the code it gives error-
\System.FormatException: Invalid length for a Base-64 char array or string.

My code:

byte[] bDoccontent = null;
HttpWebRequest hwrProg = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse hwrpProg = (HttpWebResponse)hwrProg.GetResponse();
StreamReader sr = new StreamReader(hwrpProg.GetResponseStream(), Encoding.ASCII);
bDoccontent = Convert.FromBase64String(sr.ReadToEnd());
sr.Close();
Response.Clear();
Response.ContentType = "Application/pdf";
Page.Response.AddHeader("Content-Disposition", "inline;filename=wei.pdf");
Response.AddHeader("content-length", bDoccontent.Length.ToString());
Response.BinaryWrite(bDoccontent);
Response.Flush();
Response.Close();


Please help me :(

解决方案

The error means that the string returned by ReadToEnd method is not a Base64 string...
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^]


You need to look at exactly what the web request is returning: it isn't just a Base64 string, there is presumably some packaging around it.
Use the debugger, or log the raw data to a file and examine it: there is stuff I think you need to remove - but we can't do that for you as we have no access to your code or the URL...


You should read an array of chars from the stream and then to finalize your stream reader before to do the conversion.

byte[] bDoccontent = null;
HttpWebRequest hwrProg = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse hwrpProg = (HttpWebResponse)hwrProg.GetResponse();
StreamReader sr = new StreamReader(hwrpProg.GetResponseStream(), Encoding.ASCII);
char[] base64CharArray = new char[sr.BaseStream.Length];//New
sr.Read(base64CharArray, 0, (int)sr.BaseStream.Length); //New
sr.Close(); //New
string base64String = new string(base64CharArray); //New
bDoccontent = Convert.FromBase64String(base64String );//New
Response.Clear();
Response.ContentType = "Application/pdf";
Page.Response.AddHeader("Content-Disposition", "inline;filename=wei.pdf");
Response.AddHeader("content-length", bDoccontent.Length.ToString());
Response.BinaryWrite(bDoccontent);
Response.Flush();
Response.Close();


这篇关于System.FormatException:Base-64 char数组或字符串的长度无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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