【发布时间】:2010-01-11 21:51:27
【问题描述】:
我有一个网络应用程序,我将图像上传到该应用程序,该图像随后保存到服务器的临时文件夹并通过 webhandler 显示在 aspx 页面上。
aspx 的代码:
<img src="PreviewImageQualityHandler.ashx" alt="Picture not loaded" runat="server" id="imagePreview" />
上传图片并为Session添加唯一id的代码:
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>");
}
}
webhandler 代码:
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 相关的所有内容,它会在每次回发时触发,就像它应该的那样。
如果有人能够解释我的问题,我将非常感激!
问候
弗朗西斯
【问题讨论】:
-
不幸的是,弗朗西斯,我似乎无法复制您所看到的错误。也许您发布的代码之外有一些东西会导致问题?我的赌注是在某处缓存。不知道是什么或在哪里。
标签: c# asp.net session dynamic-data