-1

我正在使用 Azure 开发 Asp.Net MVC 应用程序。当我将 PDF 文档上传到 Azure blob 存储时,它将使用以下代码完美上传。

           var filename = Document.FileName;
           var contenttype = Document.ContentType;

           int pdfocument = Request.ContentLength;

        //uploading document in to azure blob

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

           var storageAccount = CloudStorageAccount.DevelopmentStorageAccount(FromConfigurationSetting("Connection"));
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
           CloudBlobContainer container = blobClient.GetContainerReference("containername");
           container.CreateIfNotExists();
            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            container.SetPermissions(permissions);
            string uniqueBlobName = string.Format(filename );
            CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
            blob.Properties.ContentType = ;
            blob.UploadFromStream(Request.InputStream);

将文档上传到 blob 后,尝试读取 pdf 文档时出现错误“未找到 PDF 标头签名”。该错误代码是

          byte[] pdf = new byte[pdfocument];
          HttpContext.Request.InputStream.Read(pdf, 0, pdfocument);               
          PdfReader pdfReader = new PdfReader(pdf);     //error getting here           

还有一件事我忘记了,即如果我们评论上面的代码(将文档上传到 Azure blob),那么不会收到该错误。

4

1 回答 1

0

在您的组合用例中,您尝试读取 Request.InputStream 两次,一次是在上传期间,一次是稍后尝试将其读入您的byte[] pdf--- 当您第一次阅读它时,您一直阅读它直到它结束,所以第二次阅读很可能是根本没有得到任何数据。

正如您无论如何都打算将 PDF 读入内存(上述byte[] pdf),您可以在您的组合用例中

  • 首先将数据读入该数组

    int pdfocument = Request.ContentLength;
    byte[] pdf = new byte[pdfocument];
    HttpContext.Request.InputStream.Read(pdf, 0, pdfocument);
    
  • 然后使用CloudBlob.UploadByteArray上传该数组

    var storageAccount = CloudStorageAccount.DevelopmentStorageAccount(FromConfigurationSetting("Connection"));
    [...]
    CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
    blob.Properties.ContentType = ; // <--- something missing in your code...
    blob.UploadByteArray(pdf);      // <--- upload byte[] instead of stream
    
  • 然后喂你的PDF阅读器

    PdfReader pdfReader = new PdfReader(pdf);
    

这样你只读取一次流,并且一个 byte[] 应该是可重用的......

于 2013-02-16T09:31:15.437 回答