为什么我的自定义交付的图像没有缓存在浏览器中?

发布于 2024-07-23 14:05:02 字数 1156 浏览 3 评论 0原文

我有一个自定义处理程序,它将图像返回到浏览器。

图像是从数据库中获取的。

由于某种原因,浏览器没有缓存图像,我想知道是否有人能够发现我在下面的代码中缺少的内容:

HttpContext.Current.Response.BinaryWrite(imageBytes);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
Context.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
if(imgRepGetCache.DateCached.HasValue)
    HttpContext.Current.Response.Cache.SetLastModified(imgRepGetCache.DateCached.Value);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(2));
HttpContext.Current.Response.ContentType = "image/jpeg";

或者如果我完全错过了要点,并且还有其他我需要的地方看看。

编辑:根据更多信息的请求:

  • URL始终相同
  • 我正在测试通过标准IIS管道加载相同的文件,并且我的管道在同一台PC上的同一浏览器中加载。 通常通过 IIS 加载的文件会被缓存,而我的文件则不会。

编辑2:检查正常IIS路由上的HTTP请求/响应后,我认为这与ETag有关。 ETag(我现在还不熟悉)似乎是文档的一种校验和。 在浏览器的后续请求中,将发送 ETag,如果服务器发现 ETag 未更改,则返回 304 - 未修改。 都好! 但我现在使用以下方式设置 ETag:

HttpContext.Current.Response.Cache.SetETag(imgRepGetCache.DateCached.ToString());

但它不会出现在响应中。 更近一点...

编辑 3: 在利用 Firebug 进行一些 HTTP 检查的乐趣后,我最终修复了它。 我在下面发布了我的解决方案。

I have a custom handler that is returning an image to the browser.

The images are fetched from a database.

For some reason the images are not being cached by the browser, and I was wondering if someone might be able to spot what I am missing from the below code:

HttpContext.Current.Response.BinaryWrite(imageBytes);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
Context.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
if(imgRepGetCache.DateCached.HasValue)
    HttpContext.Current.Response.Cache.SetLastModified(imgRepGetCache.DateCached.Value);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(2));
HttpContext.Current.Response.ContentType = "image/jpeg";

Or alternatively if I'm completely missing the point somehow and there's somewhere else I need to look.

Edit: As per request for more info:

  • The URL is always the same
  • I am testing loading the same file via standard IIS pipe and my pipe in the same browser on the same PC. The one that loads through IIS normally is cached, my file isn't.

Edit 2: After inspecting the HTTP requests/responses on the normal IIS route I think it has something to do with the ETag. The ETag (which I'm new to as of just now) seems to be a sort of checksum for the document. On subsequent requests by a browser the ETag is sent and if the server finds the ETag hasn't changed then it returns a 304 - Not Modified. All good! But I'm now setting the ETag using:

HttpContext.Current.Response.Cache.SetETag(imgRepGetCache.DateCached.ToString());

But it doesn't appear in the response. Closer...

Edit 3: I fixed it in the end after taking advantage of Firebug for some HTTP inspecting fun. I posted my solution below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吻泪 2024-07-30 14:05:02

您在帖子中没有提及任何相关内容,但这是 https:// 地址吗? 出于安全原因,浏览器不会缓存来自 https 站点的图像和页面。

You don't mention anything in your post about it, but is this a https:// address? Browsers don't cache images and pages from https sites due to security reasons.

月亮坠入山谷 2024-07-30 14:05:02

好的,我修好了。

以下是我为其他人和我自己的未来所做的参考:

// Check for repeated request for the same image from a browser
if (HttpContext.Current.Request.Headers.Get("If-None-Match") == imgRepGetCache.DateCached.Value.ToString())
{
    // Return 304 - Not Modified
    HttpContext.Current.Response.Status = "304 Not Modified";
}
else
{
    if (imgRepGetCache.DateCached.HasValue)
        HttpContext.Current.Response.Headers.Set("Etag", imgRepGetCache.DateCached.Value.ToString());
    // ... do my other stuff here
}

很有魅力!

如果有人在这里发现任何潜在的问题,请告诉我,以便我可以更新。

为了抢先一个明显的问题 - 我可以 100% 依赖日期字符串来识别图像是否是新的(在我的特定场景中)。

OK, I fixed it.

Here is what I did for anyone else and for my own future reference:

// Check for repeated request for the same image from a browser
if (HttpContext.Current.Request.Headers.Get("If-None-Match") == imgRepGetCache.DateCached.Value.ToString())
{
    // Return 304 - Not Modified
    HttpContext.Current.Response.Status = "304 Not Modified";
}
else
{
    if (imgRepGetCache.DateCached.HasValue)
        HttpContext.Current.Response.Headers.Set("Etag", imgRepGetCache.DateCached.Value.ToString());
    // ... do my other stuff here
}

Works a charm!

If anyone spots any potential problems here, let me know so I can update this.

To pre-empt one obvious one - I can 100% rely on the date string for identifying whether an image is new or not (in my particular scenario).

野却迷人 2024-07-30 14:05:02

在生成响应时需要担心的事情有:

  • ETag
  • Expires

在接收请求时需要担心的事情有:

  • Last-Modified
  • If-Match
  • If-None-Match
  • If-Modified-Since
  • If-Unmodified -Since
  • except-Modified-Since

您可能还需要担心以下 http 方法:

  • GET
  • HEAD

这是一个应该很容易重构以满足您的需求的解决方案:
http://code.google.com/p/talifun-web/wiki/ StaticFileHandler

它从文件系统读取文件并将它们放入内存中的缓存中,因此只需将其更改为从数据库读取即可。 应该是一件轻松的工作。

The things you need to worry about in the generation of the response are:

  • ETag
  • Expires

The things that you need to worry about when receiving a request are:

  • Last-Modified
  • If-Match
  • If-None-Match
  • If-Modified-Since
  • If-Unmodified-Since
  • Unless-Modified-Since

You might also need to worry about the following http methods:

  • GET
  • HEAD

Here is a solution that should be rather easy to refactor to suite your needs:
http://code.google.com/p/talifun-web/wiki/StaticFileHandler

It reads files from the file system and them places them into a cache in memory, so just change it to read from database. Should be an easy job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文