[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Jerry J

11/6/2006 3:13:00 PM


How can I get an asp:Image to refresh when a user uploads a different jpg.

I disabled caching using this command on Page_Load():
Response.Cache.SetCacheability(HttpCacheability.No Cache);
but it didn't help.


The problem is that this page does not change images after a client uploads
a new one. If I hit the browser's refresh, then I can see the updated image,
but if I don't refresh, then I see the previous image.


The code to change the image is this simple: imgControl.ImageUrl =
"http://imagePath....

The new image does have the same name as the old image, but why should this
matter? If the browser is caching then why does it work using a refresh,
wouldn't it still use the image from the cache?


Jerry J


--
Jerry J
--
Jerry J
1 Answer

offwhite

11/7/2006 6:59:00 PM

0

In ASP.NET the markup for the page is controlled by the server-side
cache mechanism. Images are not handled by the .NET runtime. Instead
that is controlled by IIS. In this case, you want a new image to
appear for the client once it is changed at the server. The reason it
is not changing is because the client has cached the image.

To force it to get a fresh copy, you could append a query string to the
Url for the image. In an asp:Image control, you would set the ImageUrl
property. You can check the modification time of the image file and if
it is less than 5 days you could append a time stamp with the query
string. I would not always place query string in there because it will
increase the size of your page. It would look like this...

image2.jpg?ts=9199123912

Here is a code snippet in VB.NET to do that.

Dim imageUrl As String = "image.jpg"
Dim imageFile As New FileInfo(Request.MapPath("~/image.jpg"))
If (imageFile.LastWriteTime > Date.Today.AddDays(-5)) Then
imageUrl = String.Format("image.jpg?ts={0}", _
imageFile.LastWriteTime.Ticks)
End If
Image1.ImageUrl = imageUrl

Brennan Stehling
http://brennan.offwhite...

Jerry J wrote:
> How can I get an asp:Image to refresh when a user uploads a different jpg.
>
> I disabled caching using this command on Page_Load():
> Response.Cache.SetCacheability(HttpCacheability.No Cache);
> but it didn't help.
>
>
> The problem is that this page does not change images after a client uploads
> a new one. If I hit the browser's refresh, then I can see the updated image,
> but if I don't refresh, then I see the previous image.
>
>
> The code to change the image is this simple: imgControl.ImageUrl =
> "http://imagePath....
>
> The new image does have the same name as the old image, but why should this
> matter? If the browser is caching then why does it work using a refresh,
> wouldn't it still use the image from the cache?
>
>
> Jerry J
>
>
> --
> Jerry J
> --
> Jerry J