Refreshing an image

M

Mark Denardo

This is related to another post I submitted, but I'll be more precise this
time.

I have a web page that contains an Image webcontrol that loads its image by:
Image1.ImageUrl="<username>.jpg", and an option for the user to update the
photo. If they click the update button, I then flip up a new panel, where I
have a FileUpload control and corresponding Upload Button. If they load an
image file and click the Upload button, my code uses the
FileUpload1.SaveAs(...), saving to "<username>_temp.jpg", and then load the
image in another Image control (Image2) that I use for a preview area for
the user. They can re-upload as many preview photos they want and each time
the FileUpload1.SaveAs(...) command saves the image to "<username>_temp.jpg"
and the preview image always updates correctly. But when the user clicks
the final "Save Image" button to return to the original panel, I'm left with
their latest photo named "<username>_temp.jpg", which I then save as
"<username>.jpg", and re-update the original Image control (Image1). The
problem is, the original Image control, still keeps pointing to the cached
Image I loaded when the page originally loaded.

So my question is, how do I dynamically (in code) delete the temp image file
that's being held in cache or cause it to expire so that the new photo gets
updated. When I use the "FileUpload1.SaveAs(...)" command, it is doing
something somehow to make my preview Image control work properly.

Any help?
 
M

Mark E. Hansen

This is related to another post I submitted, but I'll be more precise this
time.

I have a web page that contains an Image webcontrol that loads its image by:
Image1.ImageUrl="<username>.jpg", and an option for the user to update the
photo. If they click the update button, I then flip up a new panel, where I
have a FileUpload control and corresponding Upload Button. If they load an
image file and click the Upload button, my code uses the
FileUpload1.SaveAs(...), saving to "<username>_temp.jpg", and then load the
image in another Image control (Image2) that I use for a preview area for
the user. They can re-upload as many preview photos they want and each time
the FileUpload1.SaveAs(...) command saves the image to "<username>_temp.jpg"
and the preview image always updates correctly. But when the user clicks
the final "Save Image" button to return to the original panel, I'm left with
their latest photo named "<username>_temp.jpg", which I then save as
"<username>.jpg", and re-update the original Image control (Image1). The
problem is, the original Image control, still keeps pointing to the cached
Image I loaded when the page originally loaded.

So my question is, how do I dynamically (in code) delete the temp image file
that's being held in cache or cause it to expire so that the new photo gets
updated. When I use the "FileUpload1.SaveAs(...)" command, it is doing
something somehow to make my preview Image control work properly.

Any help?

I have only a couple thoughts on this. First, the content is cached by the
client, not the server. When the content is served to the client, it gets
an expiration date/time. As far as I know, you can't tell the client to
remove any content from its cache.

What I've seen people do in other applications is name the content such
that you can simply change the name when you have updated it. For example,
name the .jpg file with a unique name, then update the HTML generation
code to use the generated name.

This way, the client can cache the content for a long period of time, and
if you decide you want the client to get new content, all you have to do
is change the name of the file.

If you change the expiration date of the content, the client will still
have the original expiration, and will not even request the new content,
so the updated expiration will never be noticed.

I'm not sure why your preview is not suffering from the same problem.
Is the file name really <username>_temp.jpg, or by "temp" did you mean
it was a unique set of characters?

If the file names are identical, then perhaps the application is serving
this temp content with a really short expiration period? If that is so,
you don't want to do the same thing with your actual content, or it will
never be able to take advantage of the cache on the client.

Hope that helps,
 
G

Guest

Mark,
as Mark H alluded, this can often be solved by making the url to the image
unque, for example:
Image1.ImageUrl ="<username>.jpg?id="+DateTime.Now.Ticks.ToString();

Peter
 
M

Mark Denardo

No the temp files will always have same same [ex. User1_temp.jpg], that's
what's so puzzling to me. What is the .SaveAs command doing that I could be
doing as well...
 
M

Mark E. Hansen

Bada Bing, that worked!

Of course, this solution will result in that particular image never
being cached - even if you never change it again. This could pose
a large performance penalty.

For example, if you were to do this trick with a photo album, it
will have to serve the pictures each time you visit the page.

If that's okay for your application, then you needed do any more.

Best,
 
G

Guest

There is a way to keep the benefits of the caching, and still defeat the
cache when updating the image. That is to keep a version number for the
image, and use that in the url:

Image1.ImageUrl = "<username>.jpg?v=" + version.ToString();
 
M

Mark E. Hansen

There is a way to keep the benefits of the caching, and still defeat the
cache when updating the image. That is to keep a version number for the
image, and use that in the url:

Image1.ImageUrl = "<username>.jpg?v=" + version.ToString();

Yes, that was already recommended earlier in the thread...
 
G

Guest

Mark said:
Yes, that was already recommended earlier in the thread...

Do you mean the suggestion by Peter Bromberg? That is not at all the
same, as that defeats the caching entirely, while my suggestion only
defeats the caching when you update the image.
 
M

Mark E. Hansen

Do you mean the suggestion by Peter Bromberg? That is not at all the
same, as that defeats the caching entirely, while my suggestion only
defeats the caching when you update the image.

Actually, I was referring to my post, where in I said:
What I've seen people do in other applications is name the content such
that you can simply change the name when you have updated it. For example,
name the .jpg file with a unique name, then update the HTML generation
code to use the generated name.

The point is that it allowed the application to decide when the image
should be fetched by the client, rather than having the image never
get cached.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top