Search code examples
asp.netgeneric-handler

asp.net generic handler not sending images for the first time


I created a generic handler (.ashx) in ASP.Net to load images from database & send it to browser.

I am using a colorbox to display this in a separate page which only has an image control & calls this handler via ImageUrl property.

For the first time, Image is not being shown in the color box. For the subsequent requests, Image is getting shown. I tried debugging to find that for the subsequent requests browser is not using a round trip but showing from cache.

How do I make it show the first time as well ?

   public class DocumentViewer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string querystring = context.Request.QueryString.ToString();


            try
            {
                if (querystring != null && querystring.Trim().Length == 0)
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Error");
                    context.Response.Write("No");
                    return;
                }
                string DocCode = querystring; 

                DocumentInfoBL bl = new DocumentInfoBL();
                DataTable dt = bl.GetDocumentInfo(DocCode);
                if (dt.Rows.Count == 0)
                {

                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Error");
                    context.Response.Write("No");
                    return;
                }
                context.Response.Clear();
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetExpires(DateTime.MinValue);

              //  context.Response.ContentType = "application/octet-stream";
                context.Response.ContentType = MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));
                context.Response.AddHeader("content-disposition", "inline; filename=" + System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));
                context.Response.BinaryWrite((Byte[])dt.Rows[0]["DocumentFile"]);
                context.Response.Buffer = false;

                context.Response.Flush();

            }
            catch (Exception ex)
            {               
                context.Response.ContentType = "text/plain";
                context.Response.Write("Error");
                context.Response.Write("No");
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

Solution

  • Code that works.It is either charset or Buffer.

     public void ProcessRequest(HttpContext context)
            {
                string querystring = context.Request.QueryString.ToString();
    
    
                try
                {
                    if (querystring != null && querystring.Trim().Length == 0)
                    {
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("Error");
                        context.Response.Write("No");
                        return;
                    }
                    string DocCode = querystring;
    
                    DocumentInfoBL bl = new DocumentInfoBL();
                    DataTable dt = bl.GetDocumentInfo(DocCode);
                    if (dt.Rows.Count == 0)
                    {
    
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("Error");
                        context.Response.Write("No");
                        return;
                    }
                    context.Response.Clear();
                    context.Response.ClearContent();
                    context.Response.ClearHeaders();
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    context.Response.Cache.SetExpires(DateTime.MinValue);
    
                    context.Response.ContentType = MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));
                    context.Response.AddHeader("content-disposition", "inline; filename=" + System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));
                    context.Response.Buffer = true;
                    context.Response.Charset = "";
    
    
                    context.Response.BinaryWrite((Byte[])dt.Rows[0]["DocumentFile"]);
    
    
                    context.Response.Flush();
    
                }
                catch (Exception ex)
                {
    //Manage here
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Error");
                    context.Response.Write("No");
                    return;
                }
                context.Response.End();
            }