保存上传的二进帽数据为图片不成国
客户端(准确是手机端)向一个A页面POST图片二进制数据过来.A页面,接收保存为图片,但无法打开.请大家帮忙看看,谢谢了
A页面相关代码如下:
Stream s = Request.InputStream;
byte[] buffer = new byte[1024];//无符号 8 位整数数组
int count = 0;
StringBuilder builder = new StringBuilder();
while ((count = s.Read(buffer, 0, 1024)) > 0) //每次读取1024个字节
{
builder.Append(Encoding.Default.GetString(buffer, 0, count));
}
string data = builder.ToString();
byte[] dataArray = Encoding.Default.GetBytes(data);
string folder = System.Web.HttpContext.Current.Server.MapPath("~/log/");
string filename = folder + DateTime.Now.ToString("mmssfff") + ".jpg";
//保存为图片文件
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(dataArray, 0, dataArray.Length);
fs.Flush();
fs.Close();
最后在log目录下可以看到创建的图片文件,文件大小也差不多,但是却无法打开.
在打印收到的数据data参数值时,有HTTP协议文件头和尾部分,如下:
-----------------------------7da4e1510598
Content-Disposition: form-data; name="xfacefile"; filename="temp.tmp"
Content-Type: application/octet-stream
.
.
.
.
.
.
.
-----------------------------7da4e1510598-- --------------------编程问答--------------------
格式问题把!!! --------------------编程问答-------------------- lz
你先排查下有没有丢失字节,看发送的字节和就收的自己是否相等
另外我也做过类似的东西,你试试
--------------------编程问答-------------------- 还有人知道么? --------------------编程问答--------------------
public string ReturnPhoto(byte[] streamByte, int uid)
{
StringBuilder jsonString = new StringBuilder();
string fileName = Guid.NewGuid().ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save(Server.MapPath("/uploads/face/" + fileName + ".png"), System.Drawing.Imaging.ImageFormat.Png);
}
//二进制流转成图片--------------------编程问答--------------------
MemoryStream stream = new MemoryStream(byteImage); // byteImage就是接收到的二进制数组
Image bitmapnew = ((Image)new Bitmap(stream));
tring folder = System.Web.HttpContext.Current.Server.MapPath("~/log/");
string filename = folder + DateTime.Now.ToString("mmssfff") + ".jpg";
bitmapnew.Save(folder+filename, ImageFormat.Jpeg);
stream.Close();
System.Drawing.Image bmp = ((System.Drawing.Image)new Bitmap(stream));
这句话好像不对...try catch 到错误:参数无效
我确定stream是有内容的.
补充:.NET技术 , ASP.NET