如何强制Web浏览器不缓存图像 [英] How to force a web browser NOT to cache images

查看:120
本文介绍了如何强制Web浏览器不缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我正在为两个公益网站编写并使用一个非常简单的基于CGI的(Perl)内容管理工具。它为网站管理员提供HTML表单,用于填充字段(日期,地点,标题,描述,链接等)并保存它们的事件。在该表单上,我允许管理员上传与该事件相关的图像。在显示表单的HTML页面上,我还显示了上传图片的预览(HTML img标签)。



问题



管理员想要更改图片时发生问题。他只需点击浏览按钮,选择一张新照片并按下确定。这个工作正常。



图片上传后,我的后端CGI会处理上传并正确地重新载入表单。



问题在于显示的图片未被刷新。即使数据库保存了正确的图像,旧图像仍会显示。我已经缩小到了网页浏览器中的IMAGE IS CACHED这一事实。如果管理员点击Firefox / Explorer / Safari中的RELOAD按钮,所有内容都会刷新并显示新图片。



我的解决方案 - 无法使用



我试图通过写一个HTTP Expires指令来控制缓存,这个指令的过去时间很长。

 过期日期:2003年9月15日星期一1:00:00 GMT 

请记住,我在管理方面,我并不在乎这些页面是否需要一些时间来加载,因为它们总是过期。



但是,这也不起作用。

注释



上传图片时,其文件名不保存在数据库中。它被重命名为 Image.jpg (当使用它时简单说明)。当用新的图像替换现有的图像时,名称也不会改变。只是图像文件的内容发生变化。



网络服务器由主机服务/ ISP提供。它使用Apache。

问题



有没有办法强制网络浏览器不从这个缓存东西页面,甚至没有图像?



我在用数据库实际保存文件名选项。这样,如果图像改变了,IMG标签的src也会改变。但是,这需要整个网站进行很多更改,如果我有更好的解决方案,我宁愿不这样做。此外,如果上传的新图片具有相同的名称(例如图片已被photoshop并重新上传),这仍然不起作用。

解决方案 Armin Ronacher有正确的想法。问题是随机串可能会发生碰撞。我会用:

 < img src =picture.jpg?1222259157.415alt => 

其中1222259157.415是服务器上的当前时间。 (注意:我用python的time.time()来生成)


Background

I am writing and using a very simple CGI-based (Perl) content management tool for two pro-bono websites. It provides the website administrator with HTML forms for events where they fill the fields (date, place, title, description, links, etc.) and save it. On that form I allow the administrator to upload an image related to the event. On the HTML page displaying the form, I am also showing a preview of the picture uploaded (HTML img tag).

The Problem

The problem happens when the administrator wants to change the picture. He would just have to hit the "browse" button, pick a new picture and press ok. And this works fine.

Once the image is uploaded, my back-end CGI handles the upload and reloads the form properly.

The problem is that the image shown does not get refreshed. The old image is still shown, even though the database holds the right image. I have narrowed it down to the fact that the IMAGE IS CACHED in the web browser. If the administrator hits the RELOAD button in Firefox/Explorer/Safari, everything gets refreshed fine and the new image just appears.

My Solution - Not Working

I am trying to control the cache by writing a HTTP Expires instruction with a date very far in the past.

Expires: Mon, 15 Sep 2003 1:00:00 GMT

Remember that I am on the administrative side and I don't really care if the pages takes a little longer to load because they are always expired.

But, this does not work either.

Notes

When uploading an image, its filename is not kept in the database. It is renamed as Image.jpg (to simply things out when using it). When replacing the existing image with a new one, the name doesn't change either. Just the content of the image file changes.

The webserver is provided by the hosting service/ISP. It uses Apache.

Question

Is there a way to force the web browser to NOT cache things from this page, not even images?

I am juggling with the option to actually "save the filename" with the database. This way, if the image is changed, the src of the IMG tag will also change. However, this requires a lot of changes throughout the site and I rather not do it if I have a better solution. Also, this will still not work if the new image uploaded has the same name (say the image is photoshopped a bit and re-uploaded).

解决方案

Armin Ronacher has the correct idea. The problem is random strings can collide. I would use:

<img src="picture.jpg?1222259157.415" alt="">

where "1222259157.415" is the current time on the server. (Note: I used python's time.time() to generate that)

这篇关于如何强制Web浏览器不缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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