简体   繁体   English

Webhandler没有在回发上开火

[英]Webhandler not firing on postback

I have a web application that I upload an image to, the image is subsequently saved to the server's temp folder and displayed via a webhandler on the aspx page. 我有一个上传图像的Web应用程序,图像随后保存到服务器的临时文件夹,并通过aspx页面上的webhandler显示。

the code for the aspx: aspx的代码:

<img src="PreviewImageQualityHandler.ashx" alt="Picture not loaded" runat="server" id="imagePreview" />

The code for uploading the picture and adding a unique id to the 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>");
    }
}

The webhandler code: 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;
        }
    }
}

My problem is that the webhandler only fires on the first image-upload. 我的问题是webhandler只在第一次上传图片时触发。 If I try and upload a new image, the webhandler never fires. 如果我尝试上传新图像,则webhandler永远不会触发。

If I remove everything to do with Session from the webhandler it fires on every single postback, like it should. 如果我从webhandler中删除与Session有关的所有内容,它就会触发每一个回发,就像它应该的那样。

If anybody is able to shed some light on my problem, I will be extremely grateful! 如果有人能够解决我的问题,我将非常感激!

Regards 问候

Francis 弗朗西斯

OK, a rewrite of my answer (see history if you haven't seen earlier answers). 好的,重写我的答案(如果你还没有看到早期的答案,请参阅历史记录)。 A few notes on your code: 关于您的代码的一些注意事项:

  1. Don't use Response.Write . 不要使用Response.Write You cannot determine when it will write and often you won't see it in the browser. 您无法确定何时写入,通常您不会在浏览器中看到它。 Use an asp:Literal control or an asp:Label instead. 请改用asp:Literal控件或asp:Label Related with current issue because: if you don't see it in the browser, perhaps you think nothing happens. 与当前问题相关的原因是:如果您在浏览器中没有看到它,也许您认为没有任何反应。

  2. You don't safe-guard the opening / closing of the file stream. 您无法保护文件流的打开/关闭。 Add a using -block around it so that the stream is properly closed and disposed off. 在它周围添加一个using -block,以便正确关闭流并将其丢弃。 The way you have it currently, the file will remain open as long as the thread is alive and that's pretty long. 你现在拥有它的方式,只要线程处于活动状态且文件很长,文件就会保持打开状态。

     using(Bitmap bmp = (Bitmap)System.Drawing.Image.FromFile(sessID)) { //.. your code... using(MemoryStream.... etc) { .... } } 

    The problem with not-closing: you will not be able to write a new file to the same location next time, you may not be able to open it for reading and you'll quite quickly run out of available file handlers when your code goes live. 非关闭的问题:下次你将无法将新文件写入同一位置,你可能无法打开它进行读取,当你的代码进入时,你很快就会用尽可用的文件处理程序生活。

  3. Not sure what you expect from the session. 不确定您对会话的期望。 But it is probably better to use some ID with the picture, store that in the database with the current user info, and add that as a request querystring to the image URL. 但是最好在图片中使用一些ID,将其与当前用户信息一起存储在数据库中,并将其作为请求查询字符串添加到图像URL。 That way, you can't get into trouble with session issues: 这样,您就不会遇到会话问题:

     <img src="myimage.ashx?id=1234" /> 

    Either, 1234 refers to something on disk, or it refers to something in the database, which in turn refers to something on disk (much safer). 或者, 1234指的是磁盘上的某些东西,或者指的是数据库中的某些东西,而这些东西又指的是磁盘上的东西(更安全)。

  4. If you are unsure whether the browser resends the request, use FireBug with FireFox, which gives you full details of the HTTP requests and responses. 如果您不确定浏览器是否重新发送请求,请使用带FireFox的FireBug,它会为您提供HTTP请求和响应的完整详细信息。 If you use the same name, the browser will (unless you're in debug mode) not send a new request each time. 如果使用相同的名称,浏览器将(除非您处于调试模式)每次都不发送新请求。

  5. You're using a catch-all exception handler. 您正在使用catch-all异常处理程序。 Write something there, or place a breakpoint at that position. 在那里写点东西,或在那个位置放一个断点。 A catch-all exception handler is very dangerous: many exceptions will never be seen. 一个包罗万象的异常处理程序非常危险:永远不会看到许多异常。 Quite possibly, your current code throws an exception (see point 2 above) and that's it. 很可能,您当前的代码会引发异常(请参阅上面的第2点),就是这样。 Remove the exception handler and the exception be caught by your Visual Studio debugger so you know what's up. 删除异常处理程序,并由Visual Studio调试程序捕获异常,以便您知道是什么。

I know, quite some issues altogether. 我知道,完全是一些问题。 Go through them one by one and see if it helps. 逐一浏览它们,看看它是否有帮助。 At the very least, your code will become more stable. 至少,您的代码将变得更加稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM