我正在使用Spreadsheet Gear生成excel文档并将其下载到客户端浏览器。使用excel文档下面的代码可以正确下载,但是我需要浏览器才能将用户重定向到当前页面之外。我要去哪里错了?
控制器
[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);
}
我尝试在RedirectToRoute
之后但在Flush
之前设置End
调用,但它仍然不会重定向。如果我是刷新前的RedirectToRoute
,那么它将重定向,但文件不会下载到浏览器。