图片缓存的解决方法。

转自http://hi.baidu.com/ucfar/blog/item/d6c3cc305d74cc10ebc4af49.html

如何取消IE里的缓存图片的问题asp.net(如何禁止图片缓存)

2009-07-28 13:11

如何取消IE里的缓存图片的问题asp.net(如何禁止图片缓存)

我使用的是 <asp:Image ID="ImageIcon" runat="server" 。。>新图片上传会覆盖原来的图片
但是此时后看到的图片仍然是原来的头像图片,除非F5一下才好。

1.
有人说设置如下即可:
(2)客户端取消

<html>
<head>
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
</head>

(3)服务器具端取消:

服务器端:
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();


这纯粹是胡扯,这些对图片是没有用的。


2.还有的说设置 Image.ImageUrl ="....jpg?id=随机数"

这个东西在aspx.cs里设置是会报错的。


最终的解决办法还是使用JS的办法。就是把图片的src = src +"id=随机数",例如:

Code
<script type="text/javascript">
var url ;
if($("#imgphoto") != "undefined")
{
url = $("#imgphoto").attr("src");
// alert(url);
$("#imgphoto").attr("src",url +"?tempid="+Math.random());
}
</script>



这样,虽然可能会瞬间闪一下老的图片,但毕竟最终显示的还是新的图片。

 

 

还有一种方法:
将你的图片文件放在单独的文件夹下,
在iis -选择图片文件夹,点击属性-http头-设置立即过期就可以了

posted on 2012-03-29 14:29  daxiaoyuyu  阅读(561)  评论(0)    收藏  举报

导航