aspx的代码如下:
<img src="PreviewImageQualityHandler.ashx" alt="Picture not loaded" runat="server" id="imagePreview" />
上传图片并在会话中添加唯一标识符的代码:
protected void uploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.FileName.EndsWith(".jpg") || FileUploadControl.FileName.EndsWith(".jpeg"))
{
string tempFileName = Path.GetTempFileName();
FileUploadControl.SaveAs(tempFileName);
Session["tempName"] = tempFileName;
Response.Write(Session["tempName"]);
fileName = FileUploadControl.FileName;
}
else
{
Response.Write("<script>alert('Please select a .jpg/.jpeg image to upload')</script>");
}
}
网络处理程序代码:
public class PreviewImageQualityHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Session.Count > 0)
{
string sessID = context.Session["tempName"].ToString();
Bitmap bmp = (Bitmap)System.Drawing.Image.FromFile(sessID);
context.Response.ContentType = "image/jpg";
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
byte[] b = ms.ToArray();
context.Response.OutputStream.Write(b, 0, b.Length);
}
}
catch(Exception ex)
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
我的问题是WebHandler仅在第一次上传图像时触发。如果我尝试上传新图像,则WebHandler永远不会触发。
如果我从WebHandler中删除与Session有关的所有内容,它就会在每个postback上触发,就像它应该做的那样。
如果有人能够解决我的问题,我将非常感激!
问候
弗朗西斯