-->

无法下载PDF文件与Rotativa在MVC4(Unable to download PDF fil

2019-10-19 23:52发布

我使用Rotativa把一个Razor视图转换成PDF。

PDF文件没有下载。

我可以看到它在提琴手,但浏览器未提示下载 - 我与IE和Chrome尝试这个。

我也试着将文件下载到使用该解决方案的物理通路这里这个问题。 但没有工作,要么因为系统无法访问该文件夹中(拒绝访问)。

这里是我的代码:

public ActionResult Index()
{
    var model = new CustomerDashboardVM();
    return View("Index", model);
}

public ActionResult print(int voucherID)
{
    var pdf =  new ActionAsPdf("Index", new { voucherID}) { FileName = "testInvoice.pdf", PageSize = Rotativa.Options.Size.A4};

  //  RotativaHelper.SaveHttpResponseAsFile("http://localhost:65425/BlankDashboard",  Server.MapPath("~\\PdfDownloads"));

    return pdf;

}

我不知道为什么会这样 - 我按一下按钮,它会调用打印ActionResult的方法 - 没有错误信息(我裹着这一个尝试,catch块)。 我有一个同事的电脑上尝试这样做,这是同样的问题!

非常感谢。

Answer 1:

问题是,我调用ActionResult print()通过Ajax岗位上的按钮单击事件的方法。 这显然是行不通的。

如果你更换一个环节,它在其URL打印()mehtod按钮它应该工作; 也就是说,现在的样子MVC环节的工作..



Answer 2:

好,我知道你的错误。 您还没有在指数方法传递的参数名称voucherID。

所以,现在你的代码如下所示:

public ActionResult Index(int voucherID)  // Here you have to pass parameter
{
    var model = new CustomerDashboardVM(voucherID);
    return View("Index", model);
}

打印()方法是这样的-

public ActionResult Print(int voucherID)
{
  return new ActionAsPdf(
                 "Index", 
                 new { voucherID = voucherID }) 
                 { 
                    FileName = "testInvoice.pdf",
                    PageSize = Rotativa.Options.Size.A4
                 };
}


文章来源: Unable to download PDF file with Rotativa in MVC4