开发者

Caching ASHX Image Response

开发者 https://www.devze.com 2023-04-08 00:30 出处:网络
I have created an ashx file to generate image thumbnails on the fly. I would like to cache these images client side after they are called the first time.

I have created an ashx file to generate image thumbnails on the fly. I would like to cache these images client side after they are called the first time.

URL:

~/image.ashx?dir=user&w=25&h=25&force=yes&img=matt.jpg

Code Behind:

public void ProcessRequest (HttpContext context) {
    TimeSpan refresh = new TimeSpan(0, 15开发者_Python百科, 0);
    context.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
    context.Response.Cache.SetMaxAge(refresh);
    context.Response.Cache.SetCacheability(HttpCacheability.Server);
    context.Response.CacheControl = HttpCacheability.Public.ToString();
    context.Response.Cache.SetValidUntilExpires(true);

    string dir = context.Request.QueryString["dir"];
    string img = context.Request.QueryString["img"];
    bool force = context.Request.QueryString["force"] == "yes";
    double w = 0;
    double h = 0;
    ...
    context.Response.ContentType = "image/jpeg";
    thumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

Using the Dev Tools in Chrome I can see the following Response Header:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 26 Sep 2011 19:17:31 GMT
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=...; path=/; HttpOnly
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: image/jpeg
Content-Length: 902
Connection: Close

Can someone enlighten me as to why this isn't caching on multiple calls with the same exact URL? Any tips would be greatly appreciated.


Surely this line:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);

Should be:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号