如何在磁盘上写FileContentResult?

时间:2014-07-25 15:13:07

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

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

  1. 为什么我需要指定一个控制器动作? ("索引",在此 案例)
  2. 如何在没有的情况下在本地磁盘上编写FileContentResult 显示它?
  3. 感谢。

    这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

你有可以直接保存到磁盘的字节数组:

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

答案 1 :(得分:0)

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

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

//提供您的路径和文件扩展名。两者都是必需的。

binary[]= YOUR DATA

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

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

这很简单......

相关问题