Cannot redirect after HTTP headers have been sent exception in asp.net C#

Cannot redirect after HTTP headers have been sent exception in asp.net C#



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!!





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 at 5:34





Hello, @Karl-JohanSjögren thankyou for your response, I just want to refresh the current page after user download the file.
– Rohit_Chiki
Aug 23 at 5: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 at 5:48





okie... I'll try thanks
– Rohit_Chiki
Aug 23 at 6:04









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)