Issue: Images with same name or URL do not get updated when file is changed in ASP.Net
 
Author:
Filed Under: Issues and Exceptions
Published Date: Sep 30, 2009
Views: 3649
 

Abstract: Here Mudassar Ahmed Khan has explained how to deal with the issue when images do not get updated even if the file is changed when the image have same name or URL

Comments:  4

 

Here I am explaining the issue when two images have same name and URL, thus even if the image is replaced the browser still displays the old image in spite of page reload and page refresh.

 

Cause

Since the images have same name and URL browser picks the image from the cache and displays the same for faster loading of the pages. Thus even if you change the image server side the same cached image is displayed until you clear the cache of the browser.

 

Solution

Now since it is not possible to clear cache of user’s browser from server side. We can use the following trick

C#

Image1.ImageUrl = "~/images/myImage.jpg?" + DateTime.Now.Ticks.ToString();

 

VB.Net

Image1.ImageUrl = "~/images/myImage.jpg?" + DateTime.Now.Ticks.ToString()

 

The idea is to append some data to the image URL so that the URL becomes unique. When the URL is unique the browser will always download the file from the server instead of loading it from cache

The following figure displays the page source which describes the output URL of the image highlighted




That’s it. Hope you liked it.









Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit