【问题标题】:How do I write FileContentResult on disk?如何在磁盘上写入 FileContentResult?
【发布时间】:2014-09-17 11:58:19
【问题描述】:

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

  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?
 }

【问题讨论】:

    标签: asp.net-mvc pdf pdf-generation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      string FileName="YOUR FILE NAME"; //首先给文件命名

      string Path=Server.MapPath("YourPath in solution"+Filename+".Pdf")
      

      //给出你的路径和文件扩展名。两者都是必需的。

      binary[]= YOUR DATA
      

      //描述要保存为文件的数据。

      System.IO.File.WriteAllBytes(Path, binary);
      

      很简单...

      【讨论】:

        猜你喜欢
        • 2013-08-10
        • 1970-01-01
        • 2016-12-12
        • 2010-11-29
        • 1970-01-01
        • 2014-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多