Exception message: Server cannot set status after HTTP headers have been sent. when generate pdf file in ASP.NET MVC3

I resolved this issue by changing my code as below:

Add below code to the return statement of your action in the controller.

return null;

public ActionResult DownloadAttachment()
        {
            try
            {
                LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/Reports/rptInvoice.rdlc");
            ReportDataSource reportDataSource = new ReportDataSource("ProductDataSet",
                new InvoiceViewModel().GetAllProductInvoice());
            localReport.DataSources.Add(reportDataSource);

            string reportType = "pdf";
            string mimeType;
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType             
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx             
            string deviceInfo =
                "<DeviceInfo>" +
                "  <OutputFormat>PDF</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            //Render the report             
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Invoice.pdf"));
                Response.BinaryWrite(content);
            }
            catch { }

            return null;
        }

Happy Coding

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Exception message: Server cannot set status after HTTP headers have been sent. when generate pdf file in ASP.NET MVC3

  1. Brian Beaird says:

    Hey – I know this is super old, but thanks for posting this. I was getting the same error and it’s because I was using Response.OutputSteam.write. Once I switched to BinaryWrite and used your code, the error went away. Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s