2016-08-24 54 views
0

我想在我的MVC项目中创建我的视图的PDF并将其保存到Azure blob存储中,而无需在我的服务器上有该文件的副本。使用rotativa创建pdf并保存到Azure blob

我正在使用Rotativa来创建我的pdf,这工作正常。

return new ActionAsPdf(ContractVersion) { FileName = "documentname.pdf" }; 

我使用下面的代码将文件保存到Azure存储。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); 
     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

     // Retrieve a reference to a container. 
     CloudBlobContainer container = blobClient.GetContainerReference("contracts"); 

     // Retrieve reference to a blob named "myblob". 
     CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob"); 

     // Create or overwrite the "myblob" blob with contents from a local file. 
     using (var fileStream = System.IO.File.OpenRead(@"c:\test blob.txt")) 
     { 
      blockBlob.UploadFromStream(fileStream); 
     } 

但我不知道如何把两种功能共同创建PDF格式,然后保存到团块。

我希望我的问题清楚。感谢预期。

回答

0

我已经下载了程序集。它只有三个构造函数。所有功能将创建PDF文件。

enter image description here

看来,我们需要保存PDF作为模板文件,然后上传到Azure的斑点。

+0

感谢您的帮助。我想我有几个选择?我可以在我的服务器上创建一个文件,将其上传到blob,然后再次删除它。或者,也许我可以尝试寻找另一种pdfwriter。什么是推荐的方法?我是这方面的新手,从来没有真正保存过服务器文件,所以我不想走错路。再次感谢。 – Ste

0

我实现它使用一个控制器的方法的MVC5局部视图,我已经缩写它为了清楚和未示出我的一个返回blob对象,因为这是标准的MS东西天青服务对象。像一种享受!

  1. 从Rotativa返回pdf;
  2. 使用“BuildPdf”转换为字节数组;
  3. 把它放在内存流中;
  4. 上传到Azure的斑点使用UploadFromStream Blob对象上

    var file = new Rotativa.PartialViewAsPdf("viewname", model) { FileName = "test" + ".pdf" }; byte[] applicationPDFData = file.BuildPdf(this.ControllerContext); Stream stream = new MemoryStream(applicationPDFData); blob.UploadFromStream(stream);