Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
File size validator for files uploaded by Webforms for marketers
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