简体   繁体   English

在不缓存跨域的情况下获取图像或其标题

[英]Get Image or its Headers Without Caching Cross-Domain

I'm trying to retrieve an image's size without caching it (for valid reasons) or get its Content-Length header without downloading the image itself.我试图在不缓存图像的情况下检索图像的大小(出于正当理由)或在不下载图像本身的情况下获取其Content-Length标头。 I need this information to match the remote image with a control one (if there is another way to achieve that with these constraints, I'm all ears).我需要这些信息来将远程图像与控制图像相匹配(如果有另一种方法可以通过这些约束实现这一点,我完全听得懂)。 The server is out of my control.服务器不在我的控制范围内。 Here is what I've tried so for:这是我尝试过的:

  1. With this script I can easily get the image, but can't prevent its caching:使用此脚本,我可以轻松获取图像,但无法阻止其缓存:

     var img = new Image(); img.src = 'http://www.nasa.gov/sites/default/themes/NASAPortal/images/nasa-logo.gif';
  2. With this I get the headers in the debugger but I can't access them:有了这个,我在调试器中得到了标题,但我无法访问它们:

     $.ajax({ url: 'http://www.nasa.gov/sites/default/themes/NASAPortal/images/nasa-logo.gif', type: 'HEAD', success: function(data){ console.log(data); } });
  3. With this I get the image without caching, but I can't access its properties:有了这个,我得到了没有缓存的图像,但我无法访问它的属性:

     <iframe src="http://www.nasa.gov/sites/default/themes/NASAPortal/images/nasa-logo.gif"></iframe>
  4. I've tried every JSONP combination, including converters and dataFilter with no success.我已经尝试了所有 JSONP 组合,包括converters和数据dataFilter convertersdataFilter没有成功。

  5. I've explored the possibility to download just the first n bytes of the image (so that caching wouldn't be a problem), but it only seems possible with AJAX, which blocks the attempt because it's cross-domain and JSONP doesn't seem to work either.我已经探索了只下载图像的前 n 个字节的可能性(这样缓存不会成为问题),但似乎只有 AJAX 才有可能,它阻止了尝试,因为它是跨域的,而 JSONP 没有似乎也有效。

In summary, I've tried everything I could read and think about... I spent the last 48 hours (and hundreds of fiddles) looking for a solution.总而言之,我已经尝试了所有我能阅读和思考的东西......我花了过去的 48 小时(和数百个小提琴)寻找解决方案。 I have the constant feeling that with the above pieces, there should be a way...我一直觉得,对于上述作品,应该有一种方法......

What am I missing?我错过了什么?

How about just adding a querystring to avoid caching如何只添加一个查询字符串来避免缓存

var img  = new Image();
var rand = (new Date()).getTime();;

img.src  = 'http://www.nasa.gov/ ... /images/nasa-logo.gif?r=' + rand ;

You can serve your images with the following response header:您可以使用以下响应标头提供图像:

Cache-Control:no-cache, no-store缓存控制:无缓存,无存储

That will prevent the browser from caching it, including proxy servers.这将阻止浏览器缓存它,包括代理服务器。

您可以获取系统毫秒并在 url 末尾添加 '?time='+milliseconds,大多数 http 缓存都以 url 为键,因此您可以在每次请求时获得新图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM