Render PDF File onto Client's Browser

Generic Procedure for Rendering given file to Client's Browser:



protected void WriteToResponse(string fileName, bool saveAsFile, string fileFormat, System.IO.MemoryStream stream)
        {
            if (Page == null Page.Response == null) return;
            string disposition = saveAsFile ? "attachment" : "inline";
            Page.Response.Clear();
            Page.Response.Buffer = false;
            Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
            Page.Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Page.Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat));
            Page.Response.BinaryWrite(stream.GetBuffer());
            Page.Response.End();
        }

        //Saves File on Server - For given Location
        protected void SaveMemoryStream(MemoryStream stream, string FileName)
        {
            FileStream outStream = File.OpenWrite(FileName);
            stream.WriteTo(outStream);
            outStream.Flush();
            outStream.Close();
        }

Usage:



MemoryStream stream = new MemoryStream();
DevExpress.Web.ASPxTreeList.Export.Helper.ASPxTreeListLink link = new DevExpress.Web.ASPxTreeList.Export.Helper.ASPxTreeListLink(treeViewPDFExporter);
link.CreateReportHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateReportHeaderArea);
link.PrintingSystem = new DevExpress.XtraPrinting.PrintingSystem();
link.CreateDocument();
link.PrintingSystem.ExportToPdf(stream);

//Renders Document to Client's Browser
WriteToResponse(DPkg_MapFile, true, "pdf", stream);

//Saves File on to Server location

SaveMemoryStream(stream, DPkg_MapFile);

Comments

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

How to get Client's Location using IPAddress