Asp.net mvc 使用“另存为”对话框进行Rotativa下载

Asp.net mvc 使用“另存为”对话框进行Rotativa下载,asp.net-mvc,rotativa,Asp.net Mvc,Rotativa,我正在使用Rotativa工具显示pdf。它适用于以下代码: public ActionResult PreviewDocument() { var htmlContent = Session["html"].ToString(); var model = new PdfInfo { Content = htmlContent, Name = "PDF Doc" }; return new ViewAsPdf(model); } 我想知道如何通过浏览器的“另存为

我正在使用Rotativa工具显示pdf。它适用于以下代码:

public ActionResult PreviewDocument()
{

     var htmlContent = Session["html"].ToString();
     var model = new PdfInfo { Content = htmlContent, Name = "PDF Doc" };
     return new ViewAsPdf(model);
}
我想知道如何通过浏览器的“另存为”对话框点击按钮下载pdf,而不是在某些iframe中显示。“new ViewAsPdf(model)”仅返回pdf数据


提前谢谢。

我终于有办法做到这一点了

实际上rotativa的方法“returnnewviewspdf(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);
}

您可以使用returnnewactionaspdf。无需自定义属性或任何其他要求。 例如:


您可以向Rotativa调用添加其他属性,如下所示:

return new PartialViewAsPdf("PreviewDocument", pdfModel)
                   {
                       PageSize = Size.A4,
                       FileName = "PDF Doc.pdf"
                   };

它将为您创建文件。:)

为什么这个答案不被称为“答案”?这对所有使用MVC ActionResults返回PDF文件的人都很有用,而不管使用哪个库。我正在使用MVCrazOrtPDF,这取决于iTextSharp,而这段代码仍然有效。这是不必要的复杂,看看@derekadk的答案。您只需添加FileName属性:returnnewviewspdf(model){FileName=“file.pdf”};为什么这不是公认的答案?它很简单,几乎不需要任何额外的代码,而且工作正常。这正是我所需要的。超级简单。谢谢
public ActionResult PrintPreviewDocument()
{
    return new ActionAsPdf("PreviewDocument") { FileName = "PDF Doc.pdf" };
}

public ActionResult PreviewDocument()
{

    var htmlContent = Session["html"].ToString();
    var model = new PdfInfo { Content = htmlContent, Name = "PDF Doc" };
    return View(model);
}
return new PartialViewAsPdf("PreviewDocument", pdfModel)
                   {
                       PageSize = Size.A4,
                       FileName = "PDF Doc.pdf"
                   };