1

我有一个带有动作的 MVC 控制器

public ActionResult GeneratePDF(string id)
{
      FileContentResult filePath = this.File(pdfBuffer, MediaTypeNames.Application.Pdf);

      return filePath;
}

并且由于某种原因,它在到达返回线时需要 20 多秒。

pdfBuffer 工作正常,当我在我的 VS 上运行它时一切正常,但是当我部署到 IIS 6 时它运行缓慢。

有谁知道为什么?

4

1 回答 1

2

我在尝试导出到 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();

最后一行实际上永远不会执行,因为我们直接在方法上结束请求。

于 2009-12-18T14:59:14.703 回答