ASP.NET: Images do not refresh when image get changed on server side

Problem:

Whenever browser find a request is being made for a image from loaded page,
browser picks the image from the cache and displays the same for faster loading of the pages.
Thus if the image get changed on server side, the Old cached image is displayed until cache of the browser is cleard.


Solution

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

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.

20 comments:

  1. THANKS! I must say that you shared very common problem that we usually got & don't know any solution. Useful post. Cheers!

    ReplyDelete
  2. excelente solución no lo había pensado desde ese punto de vista.
    gracias... desde el fin del mundo puerto Montt chile.

    ReplyDelete
  3. That worked for me. Thanks.

    ReplyDelete
  4. I am getting error like its not valid virtual path. What can i do for that

    ReplyDelete
  5. Excellent solution, thank you

    ReplyDelete
  6. Oh very good solutin thank you

    ReplyDelete
  7. there are amazing people in the world!! Thank you for sharing excellent from las vegas mevada

    ReplyDelete
  8. Great pice of code!! thank you from Las Vegas Nevada

    ReplyDelete
  9. Dhanyawad . Chumeshwari..

    ReplyDelete
  10. Excelente mi estimado muchas gracias

    ReplyDelete
  11. Thank you so much...

    ReplyDelete
  12. Hello.This article was really interesting, particularly because I was investigating for
    thougfhts on this topic last Wednesday.

    ReplyDelete
  13. is there a way to do it on android?

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. It's superior, however , check out material at the street address. visit this site

    ReplyDelete