2013-02-16 79 views
-1

我正在使用Azure处理Asp.Net MVC应用程序。当我将PDF文档上传到Azure blob存储时,它将通过使用下面的代码完美地上传。未找到PDF头标签错误?

  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头标记”。这erorr代码

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

还有一件事我忘了即如果我们评论上面的代码(在天青BLOB文件上传),那么我没有得到这个错误。

回答

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 []应该是可重复使用...

+0

安德鲁你好,现在上传字节数组进入BLOB而不是流现在出了PDF头问题。感谢您的宝贵信息很多。我想问一个这样的事情。当我在浏览器中打开PDF文档时使用它打开的BLOB URL一个弹出。弹出窗口是“此文档启用了Adobe Reader中的扩展功能。该文档自创建以来已更改,并且不再使用扩展功能。请联系作者以获取本文档的原始版本。“此错误获取Adobe Reader 9.0版本,但没有获得11.0版本 – PCSSCP 2013-02-18 06:02:38

+0

上传前是否也发生过相同的PDF?或者在上传过程中做了些什么?如果您不确定,请在上传之前和之后提供PDF。 – mkl 2013-02-18 06:12:40

+0

这是发生在未经过的时候,当我在正常打开之前上传该PDF文件时,不会弹出消息。 – PCSSCP 2013-02-18 10:14:52