A client wants to implement an image upload for user profiles last week. The file is validated when users upload for the right format and size. In MVC, the uploaded file is of the class HttpPostedFileBase.
My solution is create a few extension methods for the class HttpPostedFileBase. These methods validate whether file is of an image format, has valid minimum and maximum file size.
/// <summary> /// The http posted file base extensions. /// </summary> public static class HttpPostedFileBaseExtensions { /// <summary> /// The image minimum bytes - 512 bytes. /// </summary> public const int ImageMinimumBytes = 512; /// <summary> /// The image minimum bytes. /// </summary> public const int ImageMaximumBytes = 10 * 1024 * 1024; /// <summary> /// The allowed mime types - 10 Mb /// </summary> private static readonly List<string> ImageMimeTypes = new List<string> { "image/jpg", "image/jpeg", "image/pjpeg", "image/gif", "image/x-png", "image/png" }; /// <summary> /// The image file extensions. /// </summary> private static readonly List<string> ImageFileExtensions = new List<string> { ".jpg", ".png", ".gif", ".jpeg" }; /// <summary> /// Checks if posted file is a valid image file. /// </summary> /// <param name="postedFile"> /// The posted file. /// </param> public static void ValidateImageFile(this HttpPostedFileBase postedFile) { if (!postedFile.ValidMinimumImageSize()) { throw new ArgumentException($"Image file must be larger than {ImageMinimumBytes} bytes."); } if (!postedFile.ValidMaximumImageSize()) { throw new ArgumentException($"Image file must be smaller than {ImageMaximumBytes/(1024*1024)} MB."); } if (!postedFile.ImageFile()) { throw new ArgumentException("Uploaded file is not an image."); } } /// <summary> /// Checks if file size is larger than allowed minimum image file size. /// </summary> /// <param name="postedFile"> /// The posted file. /// </param> /// <returns> /// The <see cref="bool"/> whether file size is larger than minimum size. True is valid and False is not. /// </returns> public static bool ValidMinimumImageSize(this HttpPostedFileBase postedFile) { return postedFile.ContentLength > ImageMinimumBytes; } /// <summary> /// Checks if file size is smaller than allowed maximum image file size. /// </summary> /// <param name="postedFile"> /// The posted file. /// </param> /// <returns> /// The <see cref="bool"/> whether file size is smaller than maximum size. True is valid and False is not. /// </returns> public static bool ValidMaximumImageSize(this HttpPostedFileBase postedFile) { return postedFile.ContentLength <= ImageMaximumBytes; } /// <summary> /// The image file. /// </summary> /// <param name="postedFile"> /// The posted file. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public static bool ImageFile(this HttpPostedFileBase postedFile) { var contentType = postedFile.ContentType.ToLower(); // Check the file MIME type if (ImageMimeTypes.All(x => x != contentType)) { return false; } // Check the file extension if (ImageFileExtensions.All(x => !postedFile.FileName.EndsWith(x))) { return false; } return true; } }
Similarly, you can have additional methods in this class to validate other types of file.