合并Azure Blob(Pdf)进入一个Blob和下载到用户通过C#ASP.NET

0

的问题

我有一个ASP.NET Azure网应用程序中,涉及到用户传不同pdf入Azure Blob储存。 我想用户下载后一组合PDF包括先前载blob在一个特定的顺序。 任何想法的最佳方式做到这一点?

asp.net azure blob c#
2021-11-21 19:18:14
1

最好的答案

1

这里是2解决方法你可以试试

  1. 使用的蔚蓝的功能。
  2. 下载pdf文件蓝Blob到你的地方计算机,然后合并。

使用的蔚蓝的功能

  1. 创建蔚蓝功能的项目和使用HTTP触发。
  2. 确保你安装以下的包裹之前开始使用的编码。
  3. 创建该函数的代码。
  4. 创建Azure功能在门户网站。
  5. 发布了代码。

我们准备开始编写代码。 我们需要两个文件:

  1. ResultClass.cs –返回的合并文件(s)作为一个列表。
  2. Function1.cs –代码需要的文件名称网址,抓住他们从储存的账户,将它们合并成一个,以及返回下载的网址。

ResultClass.cs

using System;
using System.Collections.Generic;

namespace FunctionApp1
{

    public class Result
    {

        public Result(IList<string> newFiles)
        {
            this.files = newFiles;
        }

        public IList<string> files { get; private set; }
    }
}

Function1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace FunctionApp1
{
    public class Function1
    {

        static Function1()
        {

            // This is required to avoid the "No data is available                         for encoding 1252" exception when saving the PdfDocument
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

        }

        [FunctionName("Function1")]
        public async Task<Result> SplitUploadAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req,
            //container where files will be stored and accessed for retrieval. in this case, it's called temp-pdf
            [Blob("temp-pdf", Connection = "")] CloudBlobContainer outputContainer,
            ILogger log)
        {
            //get query parameters

            string uriq = req.RequestUri.ToString(); 
            string keyw = uriq.Substring(uriq.IndexOf('=') + 1);

            //get file name in query parameters
            String fileNames = keyw.Split("mergepfd&filenam=")[1];

            //split file name
            string[] files = fileNames.Split(',');

            //process merge
            var newFiles = await this.MergeFileAsync(outputContainer, files);

            return new Result(newFiles);

        }

        private async Task<IList<string>> MergeFileAsync(CloudBlobContainer container, string[] blobfiles)
        {
            //init instance
            PdfDocument outputDocument = new PdfDocument();

            //loop through files sent in query
            foreach (string fileblob in blobfiles)
            {
                String intfile = $"" + fileblob;

                // get file
                CloudBlockBlob blob = container.GetBlockBlobReference(intfile);

                using (var memoryStream = new MemoryStream())
                {
                    await blob.DownloadToStreamAsync(memoryStream);

                    //get file content
                    string contents = blob.DownloadTextAsync().Result;
                   
                    //open document
                    var inputDocument = PdfReader.Open(memoryStream, PdfDocumentOpenMode.Import);

                    //get pages
                    int count = inputDocument.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        //append
                        outputDocument.AddPage(inputDocument.Pages[idx]);
                    }


                }
            }


            var outputFiles = new List<string>();
            var tempFile = String.Empty;

            //call save function to store output in container
            tempFile = await this.SaveToBlobStorageAsync(container, outputDocument);

            outputFiles.Add(tempFile);

            //return file(s) url
            return outputFiles;
        }

        private async Task<string> SaveToBlobStorageAsync(CloudBlobContainer container, PdfDocument document)
        {

            //file name structure
            var filename = $"merge-{DateTime.Now.ToString("yyyyMMddhhmmss")}-{Guid.NewGuid().ToString().Substring(0, 4)}.pdf";

            // Creating an empty file pointer
            var outputBlob = container.GetBlockBlobReference(filename);

            using (var stream = new MemoryStream())
            {
                //save result of merge
                document.Save(stream);
                await outputBlob.UploadFromStreamAsync(stream);
            }

            //get sas token
            var sasBlobToken = outputBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5),
                Permissions = SharedAccessBlobPermissions.Read
            });

            //return sas token
            return outputBlob.Uri + sasBlobToken;
        }
    }
}

下载pdf文件蓝Blob到你的地方计算机,然后将它们合并

 internal static void combineNormalPdfFiles()
        {
            String inputFilePath1 = @"C:\1.pdf";
            String inputFilePath2 = @"C:\2.pdf";
            String inputFilePath3 = @"C:\3.pdf";
            String outputFilePath = @"C:\Output.pdf";
            String[] inputFilePaths = new String[3] { inputFilePath1, inputFilePath2, inputFilePath3 };

            // Combine three PDF files and output.
            PDFDocument.CombineDocument(inputFilePaths, outputFilePath);
        }

参考文献:

  1. Azure功能结合PDF Blob在蔚蓝的储存帐户(Blob容器)
  2. C#合并PDF SDK:合并、合并的PDF文件C#.net,ASP.NET,视,Ajax,它,WPF
2021-11-22 05:18:46

SwethaKandikonda-万吨,这是一个惊人的解决方案和一个我已经成功地纳入到我的网站。 我的许多真诚的感谢你响应! 我工作没有与蔚蓝的功能之前的评论,但我知道这么多现在。 订购和编制上传azure blob Pdf成一个PDF些东西我几乎放弃了直到这一点。
Wallstreetguy

如果我的答案是帮助你,你可以接受它作为一个答复(点击标记的旁边回答切换其从灰色的填写中)。 这可以有利于其他社区成员。 谢谢你
SwethaKandikonda-MT

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................