我在尝试导出到 XLS 和 PDF 时遇到了类似的问题,唯一似乎可以改善响应时间的方法是直接从生成文件的类发送响应,例如:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.BufferOutput = true;
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file + ".pdf");
HttpContext.Current.Response.BinaryWrite(stream.ToArray());
HttpContext.Current.Response.Flush();
stream.Close();
HttpContext.Current.Response.End();
但是如果你这样做,你会"not all code paths return a value"
从 ActionMethod 中得到一个,以避免我们只发送一个:
return new EmptyResult();
最后一行实际上永远不会执行,因为我们直接在方法上结束请求。