Reputation: 959
I have a MVC application in c#. I have a page which contains numbers of images. Once the page is loaded then all the images are being stored in the browser's cache. But when I change the image, it does replace the old images. Since the old and new image has the same name and the images are being fetched from the browser's caches. However, if I refresh the page with Ctrl+F5 then it renders the new images. Is there any way so that I can delete only that old image from the browser's cache? I can not delete all the cache of the browser, since its contains many images which should be cached. Only that images should be deleted from the cache which is getting changed.
Thank you very much for all of your replies. There are some point which I should mention here: 1. I can not use query string because that single page contains many section with different images and that particular image is rendering in the different sections. 2. I can not add the new guid or datetime with the image's url because if I do this then every time whenever page is being refreshed a new server call will happen and images will come from the server and that page rendering the same image in many places.
So to reduce the server call and to make the performance fast, I have to remove only that image from the browser cache.
Upvotes: 2
Views: 2044
Reputation: 14419
One way to accomplish this is to append a query string on the image URL that changes when the image changes. A drop dead simple one is using:
'image.jpg?' + new Date().getTime()
That would give me a URL like this:
"image.jpg?1321626971855"
Now you could this more intelligently in your JavaScript by only changing the timestamp (or adding the timestamp) when the image is changed.
Upvotes: 1
Reputation: 1911
If your saving a new image, say for example a new profile image for the user, you can use a Guid.NewGuid() as the file name (or part of the file name) in order to guarantee a unique image that will be reloaded in the browser.
Upvotes: 0
Reputation: 2227
Does the image have a unique value like a version or created datetime? If so you could add it to the filename or as a querystring. The browser would cache the images but a new value in the QueryString would request the new image.
Upvotes: 2
Reputation: 2730
not possible without going through a whole lot of trouble, believe me, you really just want to hit ctrl+f5 here ;)
Upvotes: 0