Search code examples
asp.net-mvcpdfpdf-generation

How do I write FileContentResult on disk?


I am trying to use the Rotativa component to store (not to show) a copy of the invoice permanently on web server disk. Two questions:

  1. Why I need to specify a controller action? ("Index", in this case)
  2. How do I write the FileContentResult on local disk without displaying it?

Thanks.

Here is my code:

    [HttpPost]
    public ActionResult ValidationDone(FormCollection formCollection, int orderId, bool fromOrderDetails)
    {
        Order orderValidated = context.Orders.Single(no => no.orderID == orderId);

        CommonUtils.SendInvoiceMail(orderValidated.customerID , orderValidated.orderID);

        var filePath = Path.Combine(Server.MapPath("/Temp"), orderValidated.invoiceID + ".pdf");

        var pdfResult = new ActionAsPdf("Index", new { name = orderValidated.invoiceID }) { FileName = filePath };

        var binary = pdfResult.BuildPdf(ControllerContext);

        FileContentResult fcr = File(binary, "application/pdf");

        // how do I save 'fcr' on disk?
 }

Solution

  • You do not need the FileContentResult to create a file. You've got the byte array which can be saved directly to the disk:

    var binary = pdfResult.BuildPdf(ControllerContext);
    System.IO.File.WriteAllBytes(@"c:\foobar.pdf", binary);