Last post Dec 12, 2018 08:30 AM by PatriceSc
Dec 11, 2018 03:00 PM|udal_cse|LINK
I am facing this issue while redirecting after CSV download
Here is my code
public ActionResult _ExportUnit(UnitModel model)
using (XLWorkbook wb = new XLWorkbook())
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
using (MemoryStream MyMemoryStream = new MemoryStream())
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
Dec 11, 2018 03:13 PM|bruce (sqlwork.com)|LINK
Dec 11, 2018 03:24 PM|udal_cse|LINK
Can you share some code example for that.
Thanks in advance
Dec 12, 2018 08:06 AM|Yuki Tao|LINK
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
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.
Dec 12, 2018 08:30 AM|PatriceSc|LINK
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.