Monday 9 June 2014

Partial Page Refresh after Download of file or using of Response.end in aspx page using iframe

Initially Declare a generic Handler like this
    public class Handler1 : IHttpHandler
    {

        #region IHttpHandler Members

        private HttpContext _context;
        private HttpContext Context
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }
        }


        public void ProcessRequest(HttpContext context)
        {
            Context = context;
            string filePath = context.Request.QueryString["path"];
            filePath = context.Server.MapPath(filePath);

            if (filePath == null)
            {
                return;
            }

            System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
            System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);

            byte[] bytes = new byte[streamReader.BaseStream.Length];

            br.Read(bytes, 0, (int)streamReader.BaseStream.Length);

            if (bytes == null)
            {
                return;
            }

            streamReader.Close();
            br.Close();
            string extension = System.IO.Path.GetExtension(filePath);
            string fileName = System.IO.Path.GetFileName(filePath);
            WriteFile(bytes, fileName, "application/" + extension, context.Response);
        }

        /// <summary>
        /// Sends a byte array to the client
        /// </summary>
        /// <param name="content">binary file content</param>
        /// <param name="fileName">the filename to be sent to the client</param>
        /// <param name="contentType">the file content type</param>
        private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)
        {
            response.Buffer = true;
            response.Clear();
            response.ContentType = contentType;
            response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            response.BinaryWrite(content);
            response.Flush();
            response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}

Next declare an iframe in aspx page (design)
<iframe id="iframe1 " style="display: none" runat="server" ></iframe>
and an update panel
<asp:UpdatePanel ID="UpdatePanel1 " runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
//controls design goes here
//Lets assusme we have a GridView1 here
</ContentTemplate>
</asp:UpdatePanel>
Next in aspx codebehind file write code as follows:
Some EventHandler()
{
//Method to bind GridView1 data
UpdatePanel1.Update();
   string path="Handler1.ashx?path=" + "declare your path here";
    iframe1.Attributes["src"] = path;
}
We are done:
Now we can see the page is partially refreshed when we called that event.




No comments:

Post a Comment