2012-04-17 83 views
2

我將圖像存儲在數據庫中,我使用圖像處理程序使用文件路徑+ ID顯示圖像。我遇到的問題是當我通過頁面更新圖像時,圖像不會改變。奇怪的是我沒有這個問題與Firefox或鉻。ASHX圖像處理程序不在ie中工作,在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> 

我不知道張貼什麼其他的代碼,但這裏是我認爲是相關的。當我點擊我的按鈕更新圖像時,它會成功更新數據庫中的行。此外,我可以更新有關圖像的數據並正確更新。

任何想法?

+0

你可以覈對一下頁面** ** IE使用**開發工具**,檢查圖像是否存在或不?? – 2012-04-17 13:45:02

+0

圖像顯示在ie頁面加載時,當我更新圖像時,我看到舊圖像,而不是新圖像,如果有意義的話? – 2012-04-17 13:47:52

+0

這可能是由於緩存..請看看這個** http://superuser.com/questions/81182/how-to-force-internet-explorer-ie-to-really-reload-the-page* * – 2012-04-17 13:49:38

回答

0

我最終什麼事是使用做一個GUID在我的圖片處理我的圖片ID。但在我的SQL中,當我更新圖像時,我使用sql server內置的newid()函數,因此每次更新圖像guid都會改變。通過這樣做,IE可以識別出它不是相同的ID,並且不使用緩存的圖像。

0

你可以嘗試使用這段代碼嗎?

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

IE緩存數據,並顯示相同的URL如果URL不變。我也面臨同樣的問題,並刪除它與下面的技巧: -

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

的extraQS查詢字符串DateTime.Now.Second將通過使URL動態做出的伎倆,因此URL將更改每個請求和圖像不會從瀏覽器緩存中使用。

注意: - 忽略Handler.ashx中額外的查詢字符串參數。

0

該問題可以通過在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; 
     } 
相關問題