views:

2347

answers:

4

I need to render a pdf while clicking a button.

i am using the following js code:

function RenderPDf() {
            //for
            window.open("/Resources/RenderPDF", "", 'toolbar=no,titlebar=no, directories=no, location=no,status=yes, menubar=no, resizable=yes, scrollbars=yes,width=1010, height=680,left=0,top=0');
            return false;
        }

But i am getting the following error sometimes:

Message :Server cannot set content type after HTTP headers have been sent.
Source :System.Web
Stack Trace :   at System.Web.HttpResponse.set_ContentType(String value)
  at System.Web.UI.Page.SetIntrinsics(HttpContext context, Boolean allowAsync)
  at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  at ASP.views_file_viewtaxreturn_aspx.ProcessRequest(HttpContext context)
  at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
  at System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page)
  at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)

  at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()

  at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)

  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  at System.Web.Mvc.Controller.ExecuteCore()
  at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
  at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

TargetSite :Void set_ContentType(System.String)

Please suggest with this exception.

A: 

As a suggestion, when I've had this problem it's been because I've had whitespace / new lines / html code before I started my code blocks. Check you're not sending something to the client before you call your function.

Stephen Edmonds
i dont have any space between the code.Below is the MVC C# code i am using to render:Response.Clear();Response.AddHeader("Accept-Header", PDFBytes.Length.ToString());Response.ContentType = "application/pdf";Response.AddHeader("Content-Disposition", "inline; filename=SamplePDF.pdf");Response.AddHeader("content-length", PDFBytes.Length.ToString());Response.BinaryWrite(PDFBytes);Response.Flush();
Prasad
I have to admit I've not used C# for web development. Out of interest, do you still get this problem if you move the Response.ContentType above the first line of Response.AddHeader("Accept-Header", PDFBytes.Length.ToString());
Stephen Edmonds
+1  A: 

Anything, and I mean anything sent to the client before the headers you intend to set can cause this error. Somewhere, under some conditions, your app is sending something to the client before your desired headers.

jess
+2  A: 

Solved this error by returning empty result in the controller

return new EmptyResult();

Prasad
where did you add this code?..
ifesdjeen
Its the return type of the action.
Prasad
And also I had to remove the Response.End()
ravi