nick gowdy nick gowdy - 1 year ago 128
ASP.NET (C#) Question

ASHX image handler not working in ie, works in firefox and chrome

I am storing images in a database and I am using an image handler to display the images using the file path + id. The problem I have is when I update the image through the page, the image doesn't change. Wierd thing is I don't have this problem with firefox or chrome.

ASHX handler

public void ProcessRequest(HttpContext context)
Guid image_id;
if (context.Request.QueryString["id"] != null)
image_id = System.Guid.Parse(context.Request.QueryString["id"]);
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);

User control code

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


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

I am not sure what other code to post but here is what I think is relevant. When I click my button to update an image it successfully updates the row in the database. Also I can update data about an image and this correctly updates.

Any ideas?

Answer Source

What I ended up doing is using a guid as my image id in my Image handler. But in my SQL, when I update an image I use sql servers in-built newid() function so everytime an update is made that images guid will change. By doing this IE recognises that its not the same ID and doesn't use the cached image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download