Thursday, October 07, 2010

How to serve image located outside the virtual folder

You can use GenericHander for this:
Markup:
<img src="GenericHandler.ashx?f=Thumbnail" alt="Thumbnail" />
Generic Handler:
public class GenericHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string f = context.Request.QueryString.Get("f");
        ///
        /// Option 1: from server location.
        ///
        f = @"\\servername\images\" + f + ".jpg";
        ///
        /// Option 2: from disk location
        ///
        f = @"c:\data\images\" + f + ".jpg";
        System.Drawing.Image image = System.Drawing.Image.FromFile(f);
        context.Response.Clear();
        context.Response.ClearHeaders();
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        context.Response.ContentType = "image/jpeg";
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...