最近在做一个项目,需要读取上传的图片信息与数据库中的图片做吻合比较,于是想到在上传图片的处理逻辑ashx文件中将上传的图片存储为二进制值, 再与数据库中的值做匹配测试.代码如下
//.ashxpublic class SaveHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"];
if (file != null)
{
byte[] photoValue = new byte[file.ContentLength];
file.InputStream.BeginRead(photoValue, 0, file.ContentLength, null, "1");
context.Response.Write("1");
}
else
{
context.Response.Write("0");
} } public bool IsReusable
{
get
{
return false;
}
}
}
发现取出来的photoValue在上传一张照片时没有任何问题, 但在上传超过3张图片时,就只能保存上传队列的第一张图片.其余的二进制值全为0.请教各位大神出现这个问题的原因是什么,如何解决呢?令我非常不解的是:如果在ashx中调用saveAs方法保存文件,上传超过3张图片将没有任何问题,处理代码如下:public class SaveHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"];
if (file != null)
{
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
context.Response.Write("1");
}
else
{
context.Response.Write("0");
} } public bool IsReusable
{
get
{
return false;
}
}
}
图片二进制HttpPostedFile
//.ashxpublic class SaveHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"];
if (file != null)
{
byte[] photoValue = new byte[file.ContentLength];
file.InputStream.BeginRead(photoValue, 0, file.ContentLength, null, "1");
context.Response.Write("1");
}
else
{
context.Response.Write("0");
} } public bool IsReusable
{
get
{
return false;
}
}
}
发现取出来的photoValue在上传一张照片时没有任何问题, 但在上传超过3张图片时,就只能保存上传队列的第一张图片.其余的二进制值全为0.请教各位大神出现这个问题的原因是什么,如何解决呢?令我非常不解的是:如果在ashx中调用saveAs方法保存文件,上传超过3张图片将没有任何问题,处理代码如下:public class SaveHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"];
if (file != null)
{
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
context.Response.Write("1");
}
else
{
context.Response.Write("0");
} } public bool IsReusable
{
get
{
return false;
}
}
}
图片二进制HttpPostedFile
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货