0

In my asp.net application want to refresh the current page after user has download the excel file but every time the code throws an exception says:

"Cannot redirect after HTTP headers have been sent".

I've already almost every solution on web but none of them worked for me. I've tried this approach this approach also but that also did work for me.

this is my code:

wb.Worksheets.Add(dt, "Docket_Cycle_Performance");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", 
                   "attachment;filename=DocketCyclePerformance-" 
                     + System.DateTime.Now + ".xlsx");

using (MemoryStream MyMemoryStream = new MemoryStream())
{
    wb.SaveAs(MyMemoryStream);
    MyMemoryStream.WriteTo(Response.OutputStream);
    Response.Flush();

    if (!Response.IsRequestBeingRedirected)
    {
        Response.Redirect(Request.RawUrl);
    }
    Response.End();
}  

Is there any solution of this problem.. Thanks!!

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Rohit Singh
  • 75
  • 3
  • 16
  • Why are you even redirecting them there? You send a file and then you want them to navigate away from it immediately? If you want them to just download it (instead of opening it in the browser) then the attachment; part of the disposition should be enough. – Karl-Johan Sjögren Aug 23 '18 at 05:34
  • Hello, @Karl-JohanSjögren thankyou for your response, I just want to refresh the current page after user download the file. – Rohit Singh Aug 23 '18 at 05:45
  • That wont be possible from the server. Redirecting is done by setting the Location-header and headers needs to be at the start of the response. So even if you could set the header then your file would be ignored. I would suggest that you try fixing the refresh issue with javascript instead. – Karl-Johan Sjögren Aug 23 '18 at 05:48
  • okie... I'll try thanks – Rohit Singh Aug 23 '18 at 06:04

0 Answers0