如何在不手动刷新的情况下更新图像? [英] How do I update an image without manual refresh?

查看:65
本文介绍了如何在不手动刷新的情况下更新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何获取asp:当用户上传不同的jpg时要刷新图像。


我在Page_Load上使用此命令禁用了缓存( ):

Response.Cache.SetCacheability(HttpCacheability.No Cache);

但它没有帮助。

问题在于客户端上传后,此页面不会更改图片

a new one。如果我点击浏览器的刷新,那么我可以看到更新的图像,

但如果我不刷新,那么我会看到上一张图片。

更改图像的代码很简单:imgControl.ImageUrl =

" http://imagePath.jpg" ;;


新图片确实有与旧图像同名,但为什么这个b / b $ b重要?如果浏览器正在缓存那么为什么它使用刷新工作,

它还不能使用缓存中的图像吗?

-

Jerry J


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

推荐答案

你可以欺骗浏览器从服务器获取图像,而不是来自服务器的
通过向图像源添加随机查询参数来缓存。


我使用以下javascript代码生成随机数:


函数随机( ){


return(new Date())。getMilliseconds();


}

-

Eliyahu Goldin,

软件开发人员&顾问

Microsoft MVP [ASP.NET]

" Jerry J" < Je **** @ discussion.microsoft.com写信息

news:32 ************************ ********** @ microsof t.com ...
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...

>

如何获得asp :当用户上传不同的jpg时要刷新的图像。


我在Page_Load()上使用此命令禁用了缓存:

Response.Cache.SetCacheability(HttpCacheability) 。没有缓存);

但它没有帮助。


问题是这个页面在客户端后不会改变图像

上传

a新的。如果我点击浏览器的刷新,那么我可以看到更新的

图片,

但如果我不刷新,那么我会看到上一张图片。


更改图像的代码很简单:imgControl.ImageUrl =

" http://imagePath.jpg" ;;


新图像与旧图像的名称相同,但是为什么这个

这个怎么回事?如果浏览器正在缓存那么为什么它使用刷新工作,

它是否仍然使用缓存中的图像?


-

Jerry J
>
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



On 11/06/06 07:12,Jerry J写道:
On 11/06/06 07:12, Jerry J wrote:

我如何获得一个asp:当用户上传不同的jpg时刷新图像。


我在Page_Load上使用此命令禁用了缓存():

Response.Cache.SetCacheability(HttpCacheability.No Cache);

但它没有帮助。
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.



要理解这个问题,我们需要回想一下这个图像是什么时候最初发送到客户端浏览器的
。那时,它的发送时间为

到期日期/时间,这对未来可能很有用。这个

是允许客户端缓存图像,因此它不必继续通过网络继续拉动它们。

因此,即使您更改了图像的到期时间,客户端仍然有原始到期时间的b $ b,并且甚至不会尝试获取图像的新副本。 $>
,直到到期时间到期,或客户端的缓存被刷新。


但是,有一些简单的解决方法。我更喜欢

的是简单地更改图像文件的名称(以及引用

到生成的HTML页面中的文件)。客户端的缓存基于

资源的名称,如果该名称发生变化,那么客户端

还没有它的缓存,它会下载它。


当然,为了让客户端尽可能缓存内容,你只需要
想要更改名称文件真正改变时的文件。

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.


>


问题是此页面没有改变客户上传后的图片

a new one。如果我点击浏览器的刷新,那么我可以看到更新的图像,

但如果我不刷新,那么我会看到上一张图片。


更改图像的代码很简单:imgControl.ImageUrl =

" http://imagePath.jpg" ;;


新图像与旧图像具有相同的名称,但为什么这个

重要?
>

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.


>
>


马克,


我不想改变我的形象名称。你的其他工作是什么?


感谢您的回复。


-

Jerry J

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

怎么能我得到一个asp:当用户上传不同的jpg时刷新图像。


我在Page_Load()上使用此命令禁用了缓存:

Response.Cache .SetCacheability(HttpCacheability.No Cache);

但它没有帮助。
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.



要理解这个问题,我们需要回想一下这个图像是什么时候最初发送到客户端浏览器的
。那时,它的发送时间为

到期日期/时间,这对未来可能很有用。这个

是允许客户端缓存图像,因此它不必继续通过网络继续拉动它们。

因此,即使您更改了图像的到期时间,客户端仍然有原始到期时间的b $ b,并且甚至不会尝试获取图像的新副本。 $>
,直到到期时间到期,或客户端的缓存被刷新。


但是,有一些简单的解决方法。我更喜欢

的是简单地更改图像文件的名称(以及引用

到生成的HTML页面中的文件)。客户端的缓存基于

资源的名称,如果该名称发生变化,那么客户端

还没有它的缓存,它会下载它。


当然,为了让客户端尽可能缓存内容,你只需要
想要更改名称文件真正改变时的文件。


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.




问题是客户端上传后该页面不会更改图像

a新的。如果我点击浏览器的刷新,那么我可以看到更新的图像,

但如果我不刷新,那么我会看到上一张图片。

更改图像的代码很简单:imgControl.ImageUrl =

" http://imagePath.jpg" ;;


新图片确实有与旧图像同名,但为什么这个b / b $ b重要?


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.


这篇关于如何在不手动刷新的情况下更新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆