我正在使用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标题签名”。那个erorr代码是
byte[] pdf = new byte[pdfocument];
HttpContext.Request.InputStream.Read(pdf, 0, pdfocument);
PdfReader pdfReader = new PdfReader(pdf); //error getting here
还有一件事我忘记了,即如果我们评论上面的代码(将文档上传到Azure blob中),那么我没有收到该错误。
答案 0 :(得分:0)
在组合使用案例中,您尝试读取Request.InputStream两次,一次是在上传期间,一次是在尝试将其读入您的byte[] pdf
时 - 当您首先阅读它时,您会将其读取直至结束,所以第二次阅读很可能根本没有获得任何数据。
当你打算将PDF读入内存(前面提到的byte[] pdf
)时,你可以在你的组合用例中
首先将数据读入该数组
int pdfocument = Request.ContentLength;
byte[] pdf = new byte[pdfocument];
HttpContext.Request.InputStream.Read(pdf, 0, pdfocument);
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 []应该可以重复使用......