473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I update an image without manual refresh?


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.jpg";

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
Nov 6 '06 #1
11 5638
You can trick the browser into getting the image from the server rather than
from the cache by adding a random query parameter to the image source.

I use the following javascript code to generate random numbers:

function random(){

return (new Date()).getMilliseconds();

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...
>
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.jpg";

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

Nov 6 '06 #2
On 11/06/06 07:12, 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.
To understand the problem, we need to think back to when this image was
initially sent to the client browser. At that time, it was sent with an
expiration date/time which was probably pretty for into the future. This
is to allow the client to cache the image, so it won't have to keep pulling
it down over the wire.

So, even if you change the expiration for the image, the client still has
the original expiration, and won't even try to get a new copy of the image
until the expiration time expires, or the client's cache is flushed.

However, there are a couple easy work arounds for this. The one I prefer
is to simply change the name of the image file (as well as the reference
to the file in your generated HTML page). The client's cache is based on
the name of the resource, and if that name changes, such that the client
doesn't already have it in its cache, it will download it.

Of course, to allow the client to cache the content when possible, you only
want to change the name of the file when the image really changes.

>

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.jpg";

The new image does have the same name as the old image, but why should this
matter?
Let me know if you still don't see why...
If the browser is caching then why does it work using a refresh,
wouldn't it still use the image from the cache?
As you've seen, when you issue a refresh, it causes the client to bypass
its cache. This is client specific, but I think most browsers do this.
>
Nov 6 '06 #3
Mark,

I don't want to change the name of my image. What was your other work around?

Thanks for the response.

--
Jerry J
"Mark E. Hansen" wrote:
On 11/06/06 07:12, 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.

To understand the problem, we need to think back to when this image was
initially sent to the client browser. At that time, it was sent with an
expiration date/time which was probably pretty for into the future. This
is to allow the client to cache the image, so it won't have to keep pulling
it down over the wire.

So, even if you change the expiration for the image, the client still has
the original expiration, and won't even try to get a new copy of the image
until the expiration time expires, or the client's cache is flushed.

However, there are a couple easy work arounds for this. The one I prefer
is to simply change the name of the image file (as well as the reference
to the file in your generated HTML page). The client's cache is based on
the name of the resource, and if that name changes, such that the client
doesn't already have it in its cache, it will download it.

Of course, to allow the client to cache the content when possible, you only
want to change the name of the file when the image really changes.



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.jpg";

The new image does have the same name as the old image, but why should this
matter?

Let me know if you still don't see why...
If the browser is caching then why does it work using a refresh,
wouldn't it still use the image from the cache?

As you've seen, when you issue a refresh, it causes the client to bypass
its cache. This is client specific, but I think most browsers do this.
Nov 6 '06 #4
Eliyahu,

Not sure what you mean here? What happens when the client executes that JAVA
script?

--
Jerry J
"Eliyahu Goldin" wrote:
You can trick the browser into getting the image from the server rather than
from the cache by adding a random query parameter to the image source.

I use the following javascript code to generate random numbers:

function random(){

return (new Date()).getMilliseconds();

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...

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.jpg";

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


Nov 6 '06 #5
On 11/06/06 08:20, Jerry J wrote:
Mark,

I don't want to change the name of my image. What was your other work around?
You can add parameters on to the end of the url. For example, if you're
image is accessed as http://mysite/images/myimage.jpg, you can change
it to be http://mysite/images/myimage.jpg?my-random-value

You can change the random value anytime you want the clients to bypass
the cache a fetch a new version of the image.

>
Thanks for the response.
Nov 6 '06 #6
Oh, I understand, like this ->
"http://imagePath.jpg?somethinghere=differentValueEveryTim e"

I will try that!

--
Jerry J
"Eliyahu Goldin" wrote:
You can trick the browser into getting the image from the server rather than
from the cache by adding a random query parameter to the image source.

I use the following javascript code to generate random numbers:

function random(){

return (new Date()).getMilliseconds();

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...

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.jpg";

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


Nov 6 '06 #7
The browser will think it is a different image and will get it from the
server.

It doesn't have to be in javascript. If you do it on server side, you can
use the Random class for generating random numbers and adding them to the
image url.

imgControl.ImageUrl = "http://imagePath.jpg?" +
myFunctionThatGetsRandomString();

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:5D**********************************@microsof t.com...
Eliyahu,

Not sure what you mean here? What happens when the client executes that
JAVA
script?

--
Jerry J
"Eliyahu Goldin" wrote:
>You can trick the browser into getting the image from the server rather
than
from the cache by adding a random query parameter to the image source.

I use the following javascript code to generate random numbers:

function random(){

return (new Date()).getMilliseconds();

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:32**********************************@microso ft.com...
>
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.jpg";

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



Nov 6 '06 #8
This works, thank you!

--
Jerry J
"Mark E. Hansen" wrote:
On 11/06/06 08:20, Jerry J wrote:
Mark,

I don't want to change the name of my image. What was your other work around?

You can add parameters on to the end of the url. For example, if you're
image is accessed as http://mysite/images/myimage.jpg, you can change
it to be http://mysite/images/myimage.jpg?my-random-value

You can change the random value anytime you want the clients to bypass
the cache a fetch a new version of the image.


Thanks for the response.
Nov 6 '06 #9
This works thank you!
--
Jerry J
"Eliyahu Goldin" wrote:
The browser will think it is a different image and will get it from the
server.

It doesn't have to be in javascript. If you do it on server side, you can
use the Random class for generating random numbers and adding them to the
image url.

imgControl.ImageUrl = "http://imagePath.jpg?" +
myFunctionThatGetsRandomString();

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:5D**********************************@microsof t.com...
Eliyahu,

Not sure what you mean here? What happens when the client executes that
JAVA
script?

--
Jerry J
"Eliyahu Goldin" wrote:
You can trick the browser into getting the image from the server rather
than
from the cache by adding a random query parameter to the image source.

I use the following javascript code to generate random numbers:

function random(){

return (new Date()).getMilliseconds();

}
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...

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.jpg";

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


Nov 6 '06 #10
On 11/06/06 09:17, Jerry J wrote:
This works, thank you!
Please keep in mind that if you use a unique value each time the image is
accessed, you effectively defeat the cache mechanism on the client, which
can be a serious performance issue.

You really want to change the URL for the image *only* when the content
of the image actually changes. This way, the client can continue to
cache the image when it doesn't change.

Nov 6 '06 #11
good advise. Thank you.
--
Jerry J
"Mark E. Hansen" wrote:
On 11/06/06 09:17, Jerry J wrote:
This works, thank you!

Please keep in mind that if you use a unique value each time the image is
accessed, you effectively defeat the cache mechanism on the client, which
can be a serious performance issue.

You really want to change the URL for the image *only* when the content
of the image actually changes. This way, the client can continue to
cache the image when it doesn't change.

Nov 6 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: querypk | last post by:
Hi I would like to know how to resize an Image without using python Imaging library.
0
by: Michael Gorski | last post by:
In c# is there a way to print an image without viewing it? I have the location of the file, all I need to do is print the image with no interaction from the user
0
by: Klemens | last post by:
New Database. Structure created with db2look. Data for all Tables filled with load from original Database. Need for Replication to go on without full refresh. Database UDB8.1 on Win2000 Any...
0
by: Mattia | last post by:
************************************************** Manage image without exhausted memory ************************************************** Hi; I have a big problem. I must create a script that...
5
by: mattcolenutt | last post by:
Scenario: Have a product form with an "image location" field. The image property uses this field to display the image. Problem: If an image location field has been changed, the image will not...
8
by: m1post | last post by:
Hi, I'm very much a novice, and wondered if someone could help. I'll try to be as specific as possible, but don't waant you to have to read too much. I have 2 pages in the sequence. 1st allows...
5
onlymars
by: onlymars | last post by:
helloo guys, i have an image gallery page which have thumbnails at the bottom of the screen and the main image is in the middle of the page, i want that users click on the thumbnail pic's and the...
1
by: w.l.fischer | last post by:
Hi, the following sequence: set current explain mode yes; set current explain snapshot yes; update ...; set current explain mode no;
1
by: rahullko05 | last post by:
one line code to replace an image without page refresh
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.