在尝试导出到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"
为了避免这种情况,我们只发送了一个:
return new EmptyResult();
最后一行实际上永远不会执行,因为我们直接在方法上结束请求。