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

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

问题描述

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

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 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.

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

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

问题是显示的图像没有刷新.即使数据库保存了正确的图像,旧图像仍会显示.我已经将范围缩小到图像缓存在 Web 浏览器中的事实.如果管理员在 Firefox/Explorer/Safari 中点击 RELOAD 按钮,一切都会正常刷新,新图像就会出现.

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.

我试图通过编写一个带有很远过去日期的 HTTP Expires 指令来控制缓存.

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.

但是,这也不起作用.

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

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.

网络服务器由托管服务/ISP 提供.它使用 Apache.

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

有没有办法强制网络浏览器不缓存来自该页面的内容,甚至不缓存图像?

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

我正在处理使用数据库实际保存文件名"的选项.这样,如果图像发生变化,IMG 标签的 src 也会发生变化.但是,这需要在整个站点中进行大量更改,如果我有更好的解决方案,我宁愿不这样做.此外,如果上传的新图片具有相同的名称,这仍然无法正常工作(比如图片经过了一些 Photoshop 处理并重新上传).

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 有正确的想法.问题是随机字符串可能会发生冲突.我会用:

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

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

其中1222259157.415"是服务器上的当前时间.
使用 performance.now() 通过 Javascript 或使用 time.time()

Where "1222259157.415" is the current time on the server.
Generate time by Javascript with performance.now() or by Python with time.time()

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

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