How to save pdf generated by reportviewer to disk at runtime?

1

I'm doing a series of reports which I need to generate the PDF and save to the server for the user to download. I can not use atatchment mode and inline mode. Because the user has the option to create multiple reports and download later.

Follow the conversion section:

Warning[] warnings;

                string[] streamids;

                string mimeType;

                string encoding;

                string extension;



                string deviceInfo =

                  "<DeviceInfo>" +

                  "  <OutputFormat>PDF</OutputFormat>" +

                  "  <PageWidth>21cm</PageWidth>" +

                  "  <PageHeight>29.7cm</PageHeight>" +

                  "  <MarginTop>0.1in</MarginTop>" +

                  "  <MarginLeft>0in</MarginLeft>" +

                  "  <MarginRight>0in</MarginRight>" +

                  "  <MarginBottom>0.1in</MarginBottom>" +

                  "</DeviceInfo>";


                byte[] bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);



                HttpContext.Current.Response.Buffer = true;

                HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.ContentType = mimeType;

               // HttpContext.Current.Response.AddHeader("content-disposition", "C:\ExportedReport." + "PDF");
                HttpContext.Current.Response.AddHeader("content-disposition", ("atatchment; filename=ExportedReport." + "PDF"));

                HttpContext.Current.Response.BinaryWrite(bytes);

                HttpContext.Current.Response.Flush();

                HttpContext.Current.Response.End();
    
asked by anonymous 09.11.2015 / 17:16

1 answer

1

Use the File.WriteAllBytes method in the resulting array byte method Render :

System.IO.File.WriteAllBytes("C:\ExportedReport.PDF", bytes);

You will need to use a logic to create different names for PDF files and store them in some way to allow the user to download later, and you can use the HttpContext.Current.Response.WriteFile("C:\ExportedReport.PDF"); method to do this.

    
09.11.2015 / 17:27