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.
public class ImageUploadBindingModel
For additional validation control over the upload, create a custom ValidationAttribute called MaxFileSizeAttribute.
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.
Lastly the Controller handling the upload.
public async Task UploadImage(ImageUploadBindingModel model)