从DataBase缓存动态生成的图像

时间:2009-07-29 07:36:26

标签: asp.net caching

因为我们每个表单调用的次数超过50次。我怎样才能在客户端缓存图像。

对于每个刷新它调用并命中数据库。

<%@ WebHandler Language="C#" %>

using System;
using System.Web;
using System.Data.SqlClient;
using App_Code.BLL.Products;

public class ProductPicture : IHttpHandler
{
    private Product _objProducts;
    private ProductBL _objProductBL;

    #region IHttpHandler Members

    public void ProcessRequest(HttpContext context)
    {
        string strSize = context.Request.QueryString["PhotoType"];
        _objProducts = new Product();
        _objProductBL = new ProductBL();

        _objProducts.PictureID = Convert.ToInt32(context.Request.QueryString["PhotoId"]);
        SqlDataReader objReaderPhoto = _objProductBL.GetPictureByPictureID(_objProducts);
        if (!objReaderPhoto.HasRows) return;
        objReaderPhoto.Read();
        context.Response.ContentType = "Image/JPEG"; //Convert.ToString(objReaderPhoto["Photograph"]);
        context.Response.AddHeader("Content-Disposition", "attachment; filename=Image");
        switch (strSize)
        {
            case "m":
                context.Response.BinaryWrite((byte[])objReaderPhoto["PictureBinary"]);
                break;
            //case "s":
            //    context.Response.BinaryWrite((byte[]) objReaderPhoto["PhotoSearch"]);
            //    break;
            case "t":
                context.Response.BinaryWrite((byte[])objReaderPhoto["PictureThumbnail"]);
                break;
            default:
                context.Response.BinaryWrite((byte[])objReaderPhoto["PictureBinary"]);
                break;
        }

        context.Response.Cache.SetLastModified(DateTime.Now.AddYears(-1));

        objReaderPhoto.Close();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

    #endregion
}

1 个答案:

答案 0 :(得分:2)

我不认为你可以在客户端缓存它,除非它有一个URL“img / someimage.gif”,那么在HttpContext缓存中缓存结果呢?对Cache

的一点帮助