I'm using Spreadsheet Gear to generate and download an excel document to the clients browser. Using the code below the excel document downloads correctly but I need to browser to then redirect the user away from the current page. Where am I going wrong?
Controller
[HttpPost]
public ActionResult Checkout(Models.Checkout m, int id)
{
var model = this.CreateModel<Models.Checkout>();
if (model.Checkout().Success)
{
this.HandleExport(result.Workbook, "Inspection", new { controller = "Checkout", id = id, action = "Detail" });
}
return View(model);
}
HandleExport
public void HandleExport(IWorkbook workbook, string fileName, object routeValues)
{
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Response.AddHeader("Content-Disposition", $"attachment; filename =\"{fileName}_{ DateTime.Now:yyyy-MM-dd_hh-mm-ss}\".xlsx");
workbook.SaveToStream(HttpContext.Response.OutputStream, FileFormat.OpenXMLWorkbook);
HttpContext.Response.Flush();
HttpContext.Response.End();
HttpContext.Response.RedirectToRoute(routeValues);
}
I've tried setting the RedirectToRoute
call after Flush
but before End
and it still doesnt redirect. If I but the RedirectToRoute
before Flush then it redirects but the file doesn't download to the browser.