locked
Cannot redirect after HTTP headers have been sent. RRS feed

  • Question

  • User-1100884601 posted

    I am facing this issue while redirecting after CSV download 

    Here is my code 

    [HttpPost]
    public ActionResult _ExportUnit(UnitModel model)
    {


     using (XLWorkbook wb = new XLWorkbook())
                        {
                            
                            wb.Worksheets.Add(dtRecords);
                           // wb.SaveAs("Sample.xlsx");
                            Response.Clear();
                            Response.Buffer = true;
                            Response.Charset = "";
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            Response.AddHeader("content-disposition", "attachment;filename=Unit.xls");
                            using (MemoryStream MyMemoryStream = new MemoryStream())
                            {
                                wb.SaveAs(MyMemoryStream);
                                MyMemoryStream.WriteTo(Response.OutputStream);


                                Response.Flush();
                                Response.Clear();
                                Response.End();


                            }
                           
                        }


                    if (!Response.IsRequestBeingRedirected)
                    return RedirectToAction("Index");
     
    }

    After code execute file is downloading fine but it always go to Application_Error and giving

    "Cannot redirect after HTTP headers have been sent."

    Can you help me to solve this issue.

    Thanks in advance 

    Tuesday, December 11, 2018 3:00 PM

All replies

  • User-474980206 posted
    You can not change the response headers after they have been sent (redirect is a response type). You can not do what you are trying to do. The server can only return one response, either a redirect, or content, not both.

    To do what you want, client script needs to download to a new window and then do the redirect.
    Tuesday, December 11, 2018 3:13 PM
  • User-1100884601 posted

    Can you share some code example for that. 

    Thanks in advance

    Tuesday, December 11, 2018 3:24 PM
  • User1520731567 posted

    Hi udal_cse,

    You can only return one response from your action and that will contain the file.

    You cannot also put a redirection header in it, best you can do is redirect first, then start the download.

    There's no real easy way to do that, as file downloading is a client/browser thing.

    If you really want to have such effect you have to create something on client side.

    For example you can use jquery file download which has events after download and on error. More on examples page.

    or

    There is a JavaScript technique mentioned here.

    TLDR: Create a cookie on the client, override it in the file download (e.g delete it by setting a cookie with a past expiry), keep checking every so often on the client if the cookie is gone, when it is, do a redirect.

    Best Regards.

    Yuki Tao

    Wednesday, December 12, 2018 8:06 AM
  • User753101303 posted

    Hi,

    Depending on your intent you could just drop the redirection. Content-disposition is precisely to tell the browser this ia a download and so the browser does this and the current view is left unchanged. So if your redirect is an attempt to keep the same view, it is just not needed.

    Wednesday, December 12, 2018 8:30 AM