[英]byte array to Base64String (image conversion)
I am converting an image to byte array on client side in my wp7 app and then sending it to a web service(server), the code is shown below---> 我正在将图像转换为wp7应用程序中客户端的字节数组,然后将其发送到Web服务(服务器),代码如下所示--->
client side 客户端
private void SendImage(byte[] data, long UserID)
{
Uri uri = new Uri("some uri");
IDictionary<string, object> bytesToSend = new Dictionary<string, object>();
bytesToSend.Add("ImageBytes", Convert.ToBase64String(data));
PostClient post = new PostClient(bytesToSend);
post.DownloadStringAsync(uri);
}
server side 服务器端
[HttpPost]
public bool SaveImage(object ImageBytes, Int64 UserID = 1)
{
string ImgStr = ((string[])(ImageBytes))[0];
byte[] ImgBytes = Convert.FromBase64String(ImgStr); ///<----///ERROR
Image ItemImage;
using (MemoryStream ms = new MemoryStream(ImgBytes))
{
ItemImage = Image.FromStream(ms);
}
PostUserItems(ItemImage);
return true;
}
but at receiving end an error occurring 但在接收端发生错误
invalid length for a base 64 char array 基本64个字符的数组的无效长度
Your data is coming in as a String, not a String[]. 您的数据以String而不是String []的形式输入。 You can fix the cast, so instead of 您可以修复演员表,而不是
string ImgStr = ((string[])(ImageBytes))[0];
do 做
string ImgStr = (string)ImageBytes;
If you can, change the function declaration since your function requires a String
, not an Object
. 如果可以,请更改函数声明,因为函数需要String
而不是Object
。
public bool SaveImage(String ImgStr, Int64 UserID = 1)
{
byte[] ImgBytes = Convert.FromBase64String(ImgStr);
// ...
}
The length of a base64 encoded string is always a multiple of 4. If it is not a multiple of 4, then = characters are appended until it is. base64编码的字符串的长度始终为4的倍数。如果不是4的倍数,则将=字符附加到该长度为止。 A query string of the form ?name=value has problems when the value contains = charaters (some of them will be dropped, I don't recall the exact behavior). 当值包含=字符时,形式为?name = value的查询字符串会出现问题(某些字符将被删除,我不记得确切的行为)。 You may be able to get away with appending the right number of = characters before doing the base64 decode. 在执行base64解码之前,您也许可以添加正确数量的=字符。
Edit 1 编辑1
You may find that the value of UserNameToVerify has had "+"'s changed to " "'s so you may need to do something like so; 您可能会发现UserNameToVerify的值已将“ +”更改为“”,因此您可能需要执行类似的操作;
a = a.Replace(" ","+"); a = a.Replace(“”,“ +”); This should get the length right; 这应该使长度正确;
int mod4 = a.Length % 4; int mod4 = a。长度%4; if ( mod4 > 0 ) 如果(mod4> 0)
{ {
a += new string( '=', 4 - mod4 ); 一个+ =新字符串('=',4-mod4);
} }
Of course calling UrlEncode (as in LukeH's answer) should make this all moot. 当然,调用UrlEncode(如LukeH的回答)应该可以解决所有这些问题。
I've seen this error caused by the combination of good sized viewstate and over aggressive content-filtering devices/firewalls (especially when dealing with K-12 Educational institutions). 我已经看到了这种错误,这是由于尺寸较大的视图状态和过分的内容过滤设备/防火墙(特别是与K-12教育机构打交道)相结合而引起的。
We worked around it by storing Viewstate in SQL Server. 我们通过将Viewstate存储在SQL Server中来解决此问题。 Before going that route, I would recommend trying to limit your use of viewstate by not storing anything large in it and turning it off for all controls which do not need it. 在走那条路线之前,我建议尝试通过不存储任何较大的视图状态并为不需要它的所有控件将其关闭来限制您对视图状态的使用。
References for storing ViewState in SQL Server: MSDN - Overview of PageStatePersister ASP Alliance - Simple method to store viewstate in SQL Server Code Project - ViewState Provider Model 在SQL Server中存储ViewState的参考:MSDN-PageStatePersister ASP Alliance概述-在SQL Server代码项目中存储ViewState的简单方法-ViewState提供程序模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.