如何在磁盘上写入 FileContentResult?

abe*_*nci 4 pdf asp.net-mvc pdf-generation

我正在尝试使用Rotativa组件在 Web 服务器磁盘上永久存储(不显示)发票副本。两个问题:

  1. 为什么我需要指定控制器操作?(“索引”,在这种情况下)
  2. 如何将 FileContentResult 写入本地磁盘而不显示它?

谢谢。

这是我的代码:

    [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?
 }
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 5

您不需要 FileContentResult 来创建文件。您已经获得了可以直接保存到磁盘的字节数组:

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