在天蓝色中保存生成的 PDF 文件
Save generated PDF file in azure
我在 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);
}
}
在 Visual Studio 中单击您的解决方案,然后添加 => 添加连接的服务 => Select Azure 存储,然后通过向导(如果需要,创建存储帐户 - 向导有那个选项),之后,你的解决方案将配置所有需要的设置(包括连接字符串),VS 将打开页面,其中包含有关如何在浏览器中使用 Azure 存储的详细教程。由于它包含信息和所需的代码片段,因此我不会在此处包含它(将来可能会更改,以避免弃用信息)。
Tutorial about Add Connected Service => Azure Storage functionality.
我找到了一个简单的解决方案,这就是代码的作用:
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);
}
我在 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);
}
}
在 Visual Studio 中单击您的解决方案,然后添加 => 添加连接的服务 => Select Azure 存储,然后通过向导(如果需要,创建存储帐户 - 向导有那个选项),之后,你的解决方案将配置所有需要的设置(包括连接字符串),VS 将打开页面,其中包含有关如何在浏览器中使用 Azure 存储的详细教程。由于它包含信息和所需的代码片段,因此我不会在此处包含它(将来可能会更改,以避免弃用信息)。
Tutorial about Add Connected Service => Azure Storage functionality.
我找到了一个简单的解决方案,这就是代码的作用:
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);
}