使用 javascript 重新加载缓存的图像
是否可以使用 javascript 重新加载已缓存的特定图像?
在我的网站上,用户可以上传新的头像。完成此操作后,我希望浏览器重新下载新的头像,以便用户可以看到所做的更改。
目前,我正在这样做:
$('profilePic').src = 'flash/userImage.ashx?id=12345677&type=avatar&t=' + new Date().getTime()
userImage.ashx 根据 id 和查询类型返回图像。它不使用“t”参数 - 这只是因为 url 不同,所以图像会重新加载。
这在用户上传新头像的页面上效果很好,但在网站的其他地方它仍然会使用缓存的图像。
我可以关闭用户头像的缓存,但我真的不想这样做。是否可以强制重新加载特定的缓存图像?
is it possible to use javascript to reload a particular image that is has cached?
On my site users can upload new avatars. After this is done, I want the browser to redownload the new avatar, so the user can see that the change is made.
At the moment, I'm doing it like this:
$('profilePic').src = 'flash/userImage.ashx?id=12345677&type=avatar&t=' + new Date().getTime()
userImage.ashx returns images based on id and queried type. It doesn't use the 't' parameter - this is just so the url is different, so the image reloads.
This works fine on the page where the user uploads their new avatar, but elsewhere on the site it will still use the cached image.
I could turn off caching for user avatars, but I don't really want to do this. Is it possible to force a particular cached image to reload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许使用 ETag 进行缓存是一个解决方案:
http://en.wikipedia.org/wiki/HTTP_ETag
当其他用户加载页面后头像更新时,他随图像请求一起发送的 ETag 与服务器上生成的 ETag 不同,因此应该导致 200 响应而不是 304,因此图像会重新加载。
Maybe caching with ETag is a solution:
http://en.wikipedia.org/wiki/HTTP_ETag
When a different users loads a page after the avatar update, the ETag he sends with the request for the image is different than the ETag generated on the server and should therefore result in a 200 response instead of a 304, so the image is reloaded.