How to save the PDF generated by reportviewer to disk at runtime?

Asked

Viewed 857 times

1

I am making a series of reports which I need to generate the PDF and save to the server, for the user to download. I cannot use the mode atatchment and neither inline. Because the user has the option to create multiple reports and download later.

Follow the passage of conversion:

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();

1 answer

1


Use the method File.Writeallbytes in the resulting array byte of the 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, using the method HttpContext.Current.Response.WriteFile("C:\\ExportedReport.PDF"); for that reason.

  • It didn’t work. I added this line below the line : byte[] bytes = Reportviewer1.LocalReport.Render("PDF",

  • Now you are giving the error:{"Access to the path 'C: Exportedreport.PDF' is denied." }. if it was a Txt file I would have to create it, as it is a pdf , I don’t know what to do

  • 1

    Probably the user used by IIS or IIS Express is not allowed to write to C:. If IIS, create a folder under C: and permission for the user IIS_IUSRS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.