MVC: after export to excel, index action result is not getting called

506 views Asked by At

I have one MVC application, in which export to excel functionality is given. I want to redirect Index action once export is finished. I have written below code but it not redirected to index action. what i missed here ?

output = new MemoryStream();
workbook.Write(output);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "DailyCashList.xls"));
Response.Clear();
Response.BinaryWrite(output.GetBuffer());
Response.Flush();
Response.End();

return this.RedirectToAction("Index");
1

There are 1 answers

1
Slicksim On BEST ANSWER

You can't do that I'm afraid, once you have sent the header for the download, you can't then send another set of headers to do the redirect.

There is a technique mentioned in this blog post which you can alter to do a redirect when a cookie appears from the download action.

http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/

It's not perfect, but it is one of the limitations of the http protocol I'm afraid