Caching Files in the Browser
<p>By using the below function I am caching js, css file in the browser.</p>

<p>Like wise I want to cahe the Image in the browser.</p>

<pre><code>private static void CacheOrFetchFromServer(string relativePath, string absolutePath, HttpContext context)
{
    Cache cache = HttpRuntime.Cache;
    string content;
    if (cache[relativePath] == null)
    {
        Encoding encoding = Encoding.GetEncoding(DefaultEncodingCodePage);
        CacheDependency dependency = new CacheDependency(absolutePath);

        content = File.ReadAllText(absolutePath, encoding);
        cache.Insert(relativePath, content, dependency);
    }
    else
    {
        content = cache[relativePath].ToString();
    }

    using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
    {
        sw.Write(content);
    }
}
</code></pre>

<p>I had tried the below one to cache the image. But it didn't show the image.</p>

<pre><code>private static void CacheOrFetchImageFileFromServer(string relativePath, string absolutePath, HttpContext context)
{
    string extension = System.IO.Path.GetExtension(relativePath);
    if (extension.ToUpper() == ".JPG" || extension.ToUpper() == ".PNG" || extension.ToUpper() == ".GIF" || extension.ToUpper() == ".TIFF")
    {
        Cache cache = HttpRuntime.Cache;
        System.Drawing.Image imgPhoto = null;
        if (cache[relativePath] == null)
        {
            Encoding encoding = Encoding.GetEncoding(DefaultEncodingCodePage);
            CacheDependency dependency = new CacheDependency(absolutePath);

            FileStream fs = File.OpenRead(absolutePath);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);

            MemoryStream ms = new MemoryStream(data);
            Bitmap bmp = new Bitmap(ms);

            imgPhoto = System.Drawing.Image.FromFile(absolutePath);
            cache.Insert(relativePath, bmp, dependency);
        }
        else
        {
            imgPhoto = (Image) cache[relativePath];
        }

        context.Response.Write(absolutePath);


        //using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
        //{
        //    sw.Write(absolutePath);
        //}
    }
}
</code></pre>
