为什么不?ramiramilu关于使用
window.location和是正确的
iframe。除了ASP.NET MVC3,我做过同样的事情。
我建议使用返回的控制器
FileContentResult
仅供参考,关于
FileContentResultMSDN
最后,我是如何做到的(控制器):
[HttpPost] public HttpStatusCodeResult CreateExcel() { XLWorkbook wb = new XLWorkbook(XLEventTracking.Disabled); //create Excel //Generate information for excel file // ... if (wb != null) { Session["ExcelResult"] = wb; return new HttpStatusCodeResult(HttpStatusCode.OK); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } [HttpGet] public FileContentResult ExcelResult(string reportHeader) //it's your data passed to controller { byte[] fileBytes = GetExcel((XLWorkbook)Session["ExcelResult"]); return File(fileBytes, MediaTypeNames.Application.Octet, reportHeader + ".xlsx"); }在模型中(您可以根据需要删除static,并使用实例调用它)
public static byte[] GetExcel(XLWorkbook wb){ using (var ms = new MemoryStream()) { wb.SaveAs(ms); return ms.ToArray(); }}AJAX:
$.ajax({ url: "@Url.Action("CreateExcel")", async: true, type: "POST", traditional: true, cache: false, statusCode: { 400: function () { alert("Sorry! We cannot process you request"); }, 200: function () { $("#fileHolder") .attr('src', '@Url.Action("ExcelResult")?reportHeader=' + reportHeader); } } });顺便说一句,我删除了所有异常处理程序以简化代码,但是我想您可以自己做。



