-
-
Save islaytitans/f199209a43f750463a2b to your computer and use it in GitHub Desktop.
File size validator for files uploaded by Webforms for marketers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FileSizeValiadtor : FormCustomValidator | |
{ | |
protected override bool EvaluateIsValid() | |
{ | |
bool isValid = false; | |
if (!String.IsNullOrEmpty(base.ControlToValidate)) | |
{ | |
Control control = this.FindControl(base.ControlToValidate); | |
var fileUpload = control as FileUpload; | |
if (fileUpload != null && fileUpload.HasFile) | |
{ | |
isValid = ValidateFileSize(fileUpload.PostedFile); | |
} | |
} | |
return isValid; | |
} | |
private bool ValidateFileSize(HttpPostedFile postedFile) | |
{ | |
bool valid = false; | |
int? fileSizeLimitinBytes = DetermineFileSizeLimit(); | |
if (fileSizeLimitinBytes.HasValue) | |
{ | |
int sizeInBytes = postedFile.ContentLength; | |
valid = (sizeInBytes <= fileSizeLimitinBytes); | |
} | |
else | |
{ | |
// No file size limit | |
valid = true; | |
} | |
return valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment