Windows Azure UploadFromStream No Longer Works After Porting to MVC4 - Pointers?

asp.net-mvc-4, azure-storage

Solution

I managed to fix it. For some reason reading the stream directly from the HttpPostFileBase wasn't working. Simply copy it into a new memorystream solved it. My code

public string StoreImage(string album, HttpPostedFileBase image)
    {   
        var blobStorage = storageAccount.CreateCloudBlobClient();
        var container = blobStorage.GetContainerReference("containerName");
        if (container.CreateIfNotExist())
        {
            // configure container for public access
            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);
        }

        string uniqueBlobName = string.Format("{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(image.FileName)).ToLowerInvariant();
        CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
        blob.Properties.ContentType = image.ContentType;
        image.InputStream.Position = 0;
        using (var imageStream = new MemoryStream())
        {
            image.InputStream.CopyTo(imageStream);
            imageStream.Position = 0;
            blob.UploadFromStream(imageStream);
        }

        return blob.Uri.ToString();           
    }

Problem

Updated my MVC3/.Net 4.5/Azure solution to MVC4. My code for uploading an image to blob storage appears to fail each time in the upgraded MVC4 solution. However, when I run my MVC3 solution works fine. Code that does the uploading, in a DLL, has not changed. I’ve uploaded the same image file in the MVC3 and MVC4 solution. I’ve inspected in the stream and it appears to be fine. In both instance I am running the code locally on my machine and my connections point to blob storage in cloud. Any pointers for debugging? Any known issues that I may not be aware of when upgrading to MVC4? Here is my upload code: ``` public string AddImage(string pathName, string fileName, Stream image) { var client = _storageAccount.CreateCloudBlobClient(); client.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(5)); var container = client.GetContainerReference(AzureStorageNames.ImagesBlobContainerName); image.Seek(0, SeekOrigin.Begin); var blob = container.GetBlobReference(Path.Combine(pathName, fileName)); blob.Properties.ContentType = "image/jpeg"; blob.UploadFromStream(image); return blob.Uri.ToString(); } ```

Original source