【问题标题】:ASHX image handler not working in ie, works in firefox and chromeASHX 图像处理程序在 ie 中不工作,在 firefox 和 chrome 中工作
【发布时间】:2012-04-17 13:40:49
【问题描述】:

我将图像存储在数据库中,并使用图像处理程序通过文件路径 + id 显示图像。我遇到的问题是当我通过页面更新图像时,图像不会改变。奇怪的是我在使用 Firefox 或 chrome 时没有这个问题。

ASHX 处理程序

public void ProcessRequest(HttpContext context)
    {
        Guid image_id;
        if (context.Request.QueryString["id"] != null)
            image_id = System.Guid.Parse(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        Stream strm = GetImageFromDatabase(image_id);
        if (strm != null)
        {
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
            //context.Response.BinaryWrite(buffer);
        }
    }

用户控制代码

string imagePath = "<a href=" + (Image.ImageUrl = "~/ShowImage.ashx?id=" + r["Image_id"]);

标记

<asp:UpdatePanel ID="Upd1" runat="server" UpdateMode="Always" >
    <ContentTemplate>
        <div id="mpe" style="width: 600px; padding: 5px;">
            <uc2:IMG ID="IMG1" cssclass="bodycopy" runat="server" />
        </div>
        <asp:UpdateProgress ID="upp1" runat="server" AssociatedUpdatePanelID="Upd1">
            <ProgressTemplate>
                <div id="progressBackgroundFilter">
                </div>
                <div id="modalPopup">
                    &nbsp; &nbsp; Loading...
                    <img align="middle" src="../images/Ajax/loading_1.gif" />
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>
    </ContentTemplate>
</asp:UpdatePanel>

我不确定要发布哪些其他代码,但这是我认为相关的代码。当我单击按钮更新图像时,它成功更新了数据库中的行。我还可以更新有关图像的数据,并且可以正确更新。

有什么想法吗?

【问题讨论】:

  • 你能不能用开发者工具查看IE中的页面,看看有没有图片??
  • 当我更新图像时,图像显示在加载它的页面上,我看到的是旧图像而不是新图像,如果有意义的话?
  • 这可能是由于缓存的原因。请看看这个superuser.com/questions/81182/…
  • 我还应该指出,当我删除更新面板时,这个问题不存在。但我使用更新面板来控制更新进度。

标签: asp.net updatepanel httphandler


【解决方案1】:

IE 缓存数据并在 URL 不变的情况下显示相同的数据。我也面临同样的问题,并使用以下技巧将其删除:-

imgUser.ImageUrl = "Handler.ashx?UserID=" + Convert.ToString(Session["userid"]) + "extraQS=" + DateTime.Now.Second;

带有 DateTime.Now.Second 的 extraQS 查询字符串将通过使 URL 动态化来达到目的,因此 URL 将在每次请求时更改,并且不会使用浏览器缓存中的图像。

注意:- 忽略 Handler.ashx 中额外的查询字符串参数。

【讨论】:

    【解决方案2】:

    您可以尝试使用此代码吗?

    string imagePath = "<a href='" + Page.ResolveClientUrl("~/ShowImage.ashx?id=" + r["Image_id"]) + "' />";
    

    【讨论】:

      【解决方案3】:

      我最终做的是在我的图像处理程序中使用一个 guid 作为我的图像 id。但是在我的 SQL 中,当我更新图像时,我使用 sql 服务器内置的 newid() 函数,因此每次进行更新时,图像 guid 都会发生变化。通过这样做,IE 识别出它的 ID 不同,并且不使用缓存的图像。

      【讨论】:

        【解决方案4】:

        可以通过在 QueryString 末尾添加当前秒数或随机数来解决此问题。它适用于 IE。

        string imagePath = "<a href=" + (Image.ImageUrl = "~/ShowImage.ashx?id=" + r["Image_id"] + "&timeInSec="+ DateTime.Now.Second);
        

        另一个返回图像 URL 的示例

        protected string getImageURL(int index)
                {
                    return  "ImageHandler.ashx?index=" + index + "&timeInSec=" + DateTime.Now.Second;
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-21
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 2015-02-12
          • 2017-06-07
          • 1970-01-01
          相关资源
          最近更新 更多