我终于有办法做到这一点。
实际上 rotativa 的方法“return new ViewAsPdf(model)”返回 HttpResponseStream。我们几乎无能为力的地方。但是我们可以在使用自定义属性执行操作后修改/更改响应。我们可以覆盖动作过滤器的 OnResultExecuted() 方法。
控制器的动作
[HttpGet]
[ActionDownload] //here a custom action filter added
public ActionResult DownloadDocument()
{
var htmlContent = "<h1>sachin Kumar</hi>";
var model = new PdfInfo {FtContent = htmlContent, FtName = "Populate Form"};
return new ViewAsPdf(model);
}
自定义操作过滤器:
public class ActionDownloadAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//Add content-disposition header to response so that browser understand to download as an attachment.
filterContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "Report.pdf");
base.OnResultExecuted(filterContext);
}
}