2016-08-24 57 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);