【发布时间】:2015-08-16 09:57:07
【问题描述】:
我们正在使用 Flash 将图像发送到服务器并上传图像。我们尝试这样做的方法是通过参数将字节发送到服务器,然后将字节转换为图像。
http://i.gyazo.com/fb8225af80ef465b0262d97f63bd54b2.png
在图像中,对象正在发送一些不同的信息。我不确定我是否应该只接收一点信息而不是整个对象。
到目前为止,我有发帖请求
string byteArray = Request["bytes"];
然后我正在尝试将其转换为图像
string file_name = Guid.NewGuid().ToString();
//byte[] imageBytes = Convert.FromBase64String(byteArray);
Derio.App.Model.Helper.ByteArrayToFile(file_name, Derio.App.Model.Helper.GetBytes(byteArray));
我的辅助方法看起来像 -
public static class Helper
{
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from
// a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
// close file stream
_FileStream.Close();
return "true";
}
catch (Exception _Exception)
{
// Error
return "Exception caught in process:" + _Exception.ToString();
}
}
}
我们有多种不同的方法,例如尝试将其从 Base64String 转换为图像。
我终其一生都无法弄清楚我做错了什么。
【问题讨论】:
-
你还没有告诉我们问题出在哪里,首先。 (我一点也不喜欢你在
ByteArrayToFile中“处理”异常的方式,但那是另一回事。) -
你试过
Image.FromStream(memory_stream);吗? -
神圣的异常处理程序,蝙蝠侠!
return "true";?!真的吗? -
我只是这样返回它,所以我可以将错误返回到设备,不是永久放置
-
您遇到的错误究竟是什么?在哪一行?请您用这些信息更新您的问题吗?
标签: c# image byte server bytearray