-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           

もう 1 つ忘れていたことがあります。つまり、上記のコード (ドキュメントを Azure BLOB にアップロードする) にコメントを付けると、そのエラーは発生しません。

4

1 に答える 1

0

複合ユースケースでは、 Request.InputStream を2回読み取ろうとします.1回はアップロード中に、もう1回はそれをあなたに読み込もうとするときにbyte[] pdf---最初に読み取ったときは最後まで読み取ったため、2回目の読み取りはおそらくそうでしたまったくデータを取得しません。

とにかく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);
    

この方法では、ストリームを 1 回だけ読み取ることができ、byte[] は再利用できるはずです...

于 2013-02-16T09:31:15.437 に答える