//--------------------------------------------------------------------------------------------------------------------------------------------------------------------- // return Object of FileValidationConstraints with Default File Max Size and Default Allowed MimeTypes & Extensions For Document File And ImgFile //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static FileValidationConstraints MixDocAndImgConstraints() { FileValidationConstraints Constraints = new FileValidationConstraints(); Constraints.FileMaximumBytes = DefaultMaxSize; Constraints.AllowedMimeTypes = new List <string>(); // Add Accepted MimeTypes For Microsoft Office for example : application/msword , ... Constraints.AllowedMimeTypes.AddRange(AllowedMimeTypes_MicrosoftOffice); // Add Accepted MimeTypes In General without Microsoft Office mime types for example : "text/plain", "application/pdf",... Constraints.AllowedMimeTypes.AddRange(AllowedMimeTypes_GeneralDocuments); // Add Accepted MimeTypes For Images for example : "Image/png",... Constraints.AllowedMimeTypes.AddRange(AllowedMimeTypes_Images); Constraints.AllowedExtension = new List <string>(); // Add Accepted Extensions For Microsoft Office for example : doc,docx , ... Constraints.AllowedExtension.AddRange(AllowedExtensions_MicrosoftOffice); // Add Accepted Extensions In General without Microsoft Office Extensions for example : .pdf,.txt , ... Constraints.AllowedExtension.AddRange(AllowedExtensions_GeneralDocuments); // Add Accepted Extensions For Images for example : .png, ... Constraints.AllowedExtension.AddRange(AllowedExtensions_Images); return(Constraints); }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------- //@param (int) FileMaximumBytes : Maximum Size For The File In Bytes //@param (List<string>) AllowedMimeTypes : List of Allowed Mime types to Validate Uploaded File //@param (List<string>) AllowedExtension : List of Allowed Extensions to Validate Uploaded File // //return Object of FileValidationConstraints with File Max Size and Allowed MimeTypes & Extensions For Document File //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static FileValidationConstraints CustomConstraints(int FileMaximumBytes, List <string> AllowedMimeTypes, List <string> AllowedExtension) { FileValidationConstraints Constraints = new FileValidationConstraints { FileMaximumBytes = FileMaximumBytes, AllowedMimeTypes = AllowedMimeTypes, AllowedExtension = AllowedExtension }; return(Constraints); }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------- // return Object of FileValidationConstraints with Default Image Max Size and Default Allowed MimeTypes & Extensions For Image //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static FileValidationConstraints ImgConstraints() { FileValidationConstraints Constraints = new FileValidationConstraints(); Constraints.FileMaximumBytes = DefaultMaxSize; Constraints.AllowedMimeTypes = new List <string>(); Constraints.AllowedMimeTypes.AddRange(AllowedMimeTypes_Images); Constraints.AllowedExtension = new List <string>(); Constraints.AllowedExtension.AddRange(AllowedExtensions_Images); return(Constraints); }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------- //@param (IFormFile) postedFile : the uploaded file //@param (FileValidationConstraints) constraints : this object has main Constraints to Validate the File Like Maximum file size and allowed MimeTypes & Extentions //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static bool IsValidFile(IFormFile postedFile, FileValidationConstraints constraints) { try { // List of Allowed MimeTypes and Extensions List <string> MimeTypes_Allowed = constraints.AllowedMimeTypes; List <string> Extension_Allowed = constraints.AllowedExtension; //------------------------------------------- // Check the file mime types //------------------------------------------- string contentType = postedFile.ContentType.ToLower(); if (!MimeTypes_Allowed.Any(m => m == contentType)) { return(false); } //------------------------------------------- // Check the file extension //------------------------------------------- string extension = Path.GetExtension(postedFile.FileName).ToLower(); if (!Extension_Allowed.Any(e => e == extension)) { return(false); } //------------------------------------------ //check whether the file size exceeding the limit or not //------------------------------------------ if (postedFile.Length > constraints.FileMaximumBytes) { return(false); } //--------------------------------------------------------------------------------------------------------------------------------- // Check the Real Mime Type Of File (because maybe user hacked the file and change his name and extension) //--------------------------------------------------------------------------------------------------------------------------------- //MimeTypes is Custom Class not in Asp.net Classes var actualType = MimeTypes.getMimeFromFile(postedFile); if (!MimeTypes_Allowed.Any(m => m == actualType)) { // Do Final Validation : check if its MicrosoftOfficeDocument in zip format otherwise return false bool isMSOffice = isMicrosoftOfficeDocument(actualType, contentType, extension); if (!isMSOffice) { return(false); } } return(true); } catch (Exception) { return(false); } }