Stream Code
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count == 1)
{
HttpPostedFile file = httpRequest.Files[0];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(BaseService.BlobConnection));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(documentType.Description);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = file.ContentType;
blockBlob.UploadFromStream(file.InputStream, file.ContentLength);
}
I am using two files to test with the first is a docx file that is only 12.3KB and a pdf that is 1.47MB. The docx file uploads without issue, the pdf is what generates the error.
I have only found two items that attempt to explain what is going on and I really can't make heads or tails out of them. Item 1 & Item 2
I was able to use the following code, using byte data to upload without error but when I attempted to download either file they are corrupted.
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count == 1)
{
HttpPostedFile file = httpRequest.Files[0];
byte[] buffer = null;
using (var binaryReader = new BinaryReader(file.InputStream))
{ buffer = binaryReader.ReadBytes(file.ContentLength); }
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(BaseService.BlobConnection));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(documentType.Description);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.Properties.ContentType = file.ContentType;
blockBlob.UploadFromByteArray(buffer, 0, file.ContentLength);
}
ANSWERED: The issue was in my Storage Connection string I had is selected to use HTTP connections instead of HTTPS (it's recommended for a reason). I'm not sure what that has to do with buffering to succeed, but it solved the issue and I'm now able to store uploaded files into blob storage.