为什么我的自定义交付图像缓存不在浏览器中? [英] Why isn't my custom delivered image caching in the browser?

查看:168
本文介绍了为什么我的自定义交付图像缓存不在浏览器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

编辑:根据要求获取更多信息:

As per request for more info:


  • URL始终相同

  • 我正在测试通过标准IIS管道和我的管道在同一台PC上的同一浏览器中加载相同的文件。通常通过IIS加载的那个是缓存的,我的文件不是。

编辑2:之后检查正常IIS路由上的HTTP请求/响应我认为它与ETag有关。 ETag(我刚才是新手)似乎是文档的一种校验和。在浏览器的后续请求中,ETag被发送,如果服务器发现ETag没有改变,则返回304-未修改。都好!但我现在正在设置ETag:

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

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

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

推荐答案

好的,我修好了。

以下是我为其他人做的以及我将来参考的内容:

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
}

工作魅力!

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

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

To抢先一个明显的 - 我可以100%依靠日期字符串来识别图像是否是新的(在我的特定情况下)。

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

这篇关于为什么我的自定义交付图像缓存不在浏览器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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