Unable to download PDF file with Rotativa in MVC4

混江龙づ霸主 提交于 2019-12-11 07:25:35

问题


I am using Rotativa to turn a Razor view into a PDF.

The PDF file is not downloading.

I can see it in Fiddler but the browser is not prompting for download - I have tried this with both IE and Chrome.

I also tried to download the file to a physical path using the solution in this question here. but that didn't work either because of the system couldn't access the folder (Access Denied).

Here is my code:

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;

}

I wonder why is this happening - I click the button, it calls the print ActionResult method - no error messages (I wrapped this in a try and catch block). I have tried this on a colleague PC and it was the same issue!

Many thanks.


回答1:


The issue was that I was calling the ActionResult print() method on a button click event through an Ajax post. This apparently doesn't work.

It should work if you replace the button with a link, which has the print() mehtod in its URL; i.e., just the way mvc links work..




回答2:


Okay, i got your mistake. You have not passed parameter name voucherID in Index method.

So now your code look like this:

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

and Print() method look like this -

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


来源:https://stackoverflow.com/questions/23239557/unable-to-download-pdf-file-with-rotativa-in-mvc4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!