C#获取H5页面上传图片代码

基于上一篇的H5压缩上传图片,由于图片是以二进制字符流blob的形式传过来的,所以应该想将其转成bytes类型再进行转换

public void ProcessRequest(HttpContext context) //用request来接收formdata
{
var data = string.Empty;
HttpPostedFile postedFile = context.Request.Files[0];
byte[] bytes = new byte[postedFile.ContentLength];
using (BinaryReader reader = new BinaryReader(postedFile.InputStream, Encoding.UTF8))
{
bytes = reader.ReadBytes(postedFile.ContentLength);
}
string strPath = context.Server.MapPath("upload/avatarimg/"); //context.Server.MapPath("~\\upload\\avatarimg\\");
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
//新文件
string fileName= DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpeg";
string newFile = context.Server.MapPath("upload/avatarimg/" + fileName);
File.WriteAllBytes(newFile, bytes);

}

posted @ 2017-03-29 15:36  领悟.海洋  阅读(1310)  评论(0编辑  收藏  举报