我创建了一个 PdfActionResult 类,如下所示:
public class PdfActionResult : ActionResult
{
public byte[] FileContents { get; set; }
public string FileName { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var cd = new System.Net.Mime.ContentDisposition()
{
FileName = FileName,
Inline = false,
};
context.HttpContext.Response.Buffer = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.AppendHeader("Content-Disposition", cd.ToString());
context.HttpContext.Response.ContentType = "application/pdf";
context.HttpContext.Response.BinaryWrite(FileContents);
}
}
我有一个返回 PdfActionResult 的控制器方法。这在视图中调用时工作正常,但从局部视图调用时会失败。我的猜测是它与控制器上下文有关。任何帮助,将不胜感激。谢谢。