user2114177
user2114177

Reputation: 381

Restrict caching images asp.net

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <ContentTemplate >
        <asp:Image  runat="server" id="img" style="max-width:100%"></asp:Image>

I have this image in updatePanel control. And i change it's source in the code behind like this

img.ImageUrl = "~/BackFile.ashx?ID=";

and backfile.ashx code is something like this:

Image img = Image.FromFile(getsrc());
        MemoryStream memStream = new MemoryStream();
        img.Save(memStream,
          System.Drawing.Imaging.ImageFormat.Jpeg);

        context.Response.ContentType = "image/jpeg";
        memStream.WriteTo(context.Response.OutputStream);

On my computer and oogle chrome everything worked well But then i tested it from another laptop on ie and opera and pictures stopped changing their sources. I think it 's because caching .so how can Ш restrict it? I just need pictures to refresh=)

UPD: it seems not be working in ie and opera

Upvotes: 0

Views: 64

Answers (1)

Onur Gazioğlu
Onur Gazioğlu

Reputation: 511

You must set the headers to set no-cache.

context.Response.AddHeader("Cache-Control", "no-cache");
context.Response.Expires = 0;
context.Response.Cache.SetNoStore();
context.Response.AddHeader("Pragma", "no-cache");

and you should use below to ignore cached data by browser:

img.ImageUrl = string.Format("~/BackFile.ashx?ID={0}&R={1}", <your ID>, randomNumber);

Upvotes: 1

Related Questions