An interesting customer requirement last week came up where I needed to upload an image in MVC 5 directly to Azure Blob Storage. A simplified version follows, removing some application specific logic and validation steps. To start off I first created a MVC model for image upload purposes.
ImageUploadBindingModel
1 | public class ImageUploadBindingModel |
For additional validation control over the upload, create a custom ValidationAttribute called MaxFileSizeAttribute.
MaxFileSizeAttribute
1 | public class MaxFileSizeAttribute : ValidationAttribute, IClientValidatable |
Now let’s turn our attention to the actual Azure logic which streams the upload direct from MVC memory to Azure Blob Storage. Note that CloudConfigurationManager is used to source relevant connection information required by the Azure libraries, something I hope to cover in another post as it’s a very relevant topic for Cloud first .Net solutions.
AzureStorage.cs
1 | using Microsoft.WindowsAzure; |
Lastly the Controller handling the upload.
UploadImage
1 | public async Task UploadImage(ImageUploadBindingModel model) |