So I have a few ASP.NET file upload controls in my aspx file -
<asp:FileUpload ID="fulNature" class="form-control summer" runat="server" /><asp:Labe ID="lblNature" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="fulNature" Display="Dynamic"
ErrorMessage="File size should not be greater than 1 MB." ForeColor="#FF3300" OnServerValidate="ValidateFileSize"></asp:CustomValidator>
<asp:FileUpload ID="fulIndustrial" class="form-control summer" runat="server" /><asp:Label ID="lblIndustrial" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="fulIndustrial" Display="Dynamic"
ErrorMessage="File size should not be greater than 1 MB." ForeColor="#FF3300" OnServerValidate="ValidateFileSize"></asp:CustomValidator>
I'm trying to limit file size with one C# validator -
protected void ValidateFileSize(object source, ServerValidateEventArgs args)
{
var validator = (source as CustomValidator);
string controlToValidate = validator.ControlToValidate;
FileUpload SubmittedUpload = validator.NamingContainer.FindControl(controlToValidate) as FileUpload;
if (SubmittedUpload.FileBytes.Length > 1048576) // A little padding added.
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
The issue is that the validator never fires regardless of file size. I have to be missing something simple here, but I'm just not seeing it.