将生成的PDF文件保存在azure

时间:2016-05-11 16:52:52

标签: c# asp.net azure jspdf

我在ASP.NET中有一个表单,当我在最后一步填写表单时,它会生成一个PDF文件。我用过jsPDF。 我想要的是,生成的pdf文件要在Azure存储中发送(保存),是否有人可以帮助我?

谢谢

更新:这是我正在尝试的代码,它正在运行,但它只提取文本,它不会保存pdf:

var account = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("storageaccount", 
                    "accesskey"), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("folderpath");

StringBuilder text = new StringBuilder();
string filePath = "C:\\Users\\username\\Desktop\\toPDF\\testing PDFs\\test.pdf";

if (File.Exists(filePath))
{
     PdfReader pdfReader = new PdfReader(filePath);

     for (int page = 1; page <= pdfReader.NumberOfPages; page++)
     {
         ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
         string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
         currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
         text.Append(currentText);
       }
       pdfReader.Close();

       using (MemoryStream ms = new MemoryStream())
       {
       using (var doc = new iTextSharp.text.Document())
       {
             PdfWriter writer = PdfWriter.GetInstance(doc, ms);
             doc.Open();
             doc.Add(new Paragraph(text.ToString()));
       }
       var byteArray = ms.ToArray();
       var blobName = "test.pdf";
       var blob = container.GetBlockBlobReference(blobName);
       blob.Properties.ContentType = "application/pdf";
       blob.UploadFromByteArray(byteArray, 0, byteArray.Length);
       }
    } 

2 个答案:

答案 0 :(得分:1)

我找到了一个简单的解决方案,这就是代码的作用:

string filePath = "C:\\Users\\username\\Desktop\\toPDF\\testing PDFs\\rpa.pdf";
var credentials = new StorageCredentials("storageaccount","accesskey");
var client = new CloudBlobClient(new Uri("https://jpllanatest.blob.core.windows.net/"), credentials);

// Retrieve a reference to a container. (You need to create one using the mangement portal, or call container.CreateIfNotExists())
var container = client.GetContainerReference("folderpath");
// Retrieve reference to a blob named "myfile.pdf".
var blockBlob = container.GetBlockBlobReference("myfile.pdf");

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

答案 1 :(得分:0)

在Visual Studio中单击您的解决方案,然后单击Add =&gt;添加连接服务=&gt;选择Azure存储,然后完成向导(如果需要,创建存储帐户 - 向导具有该选项),之后,您的解决方案将配置所有需要的设置(包括连接字符串),VS将打开页面有关如何在浏览器中使用Azure存储的详细教程。因为它有信息和所需的代码片段,所以我不会在这里包含它(可能会在将来改变,以避免被弃用的信息)。

Tutorial about Add Connected Service => Azure Storage functionality