troubleshooting Question

Custom validator on file size

Avatar of LearningCSharpSQL
LearningCSharpSQL asked on
.NET ProgrammingC#ASP.NET
5 Comments1 Solution586 ViewsLast Modified:
Hi
can anyone help with this problem, i'm trying to display an error message in a custom validator if a file size is larger than 200kbs, but i cannot get the validation to fire, i've tried changing the args,Isvalid to true or false but no luck.

Regards
George

<asp:FileUpload ID="FileUpload1" TabIndex="20" runat="server" />
                   <asp:CustomValidator ID="CustomValidator1"
                                        runat="server"
                                        ErrorMessage="File must be less than 200kb's"
                                        ControlToValidate="FileUpload1"
                                        Display="Dynamic"
                                        OnServerValidate="CustomValidator1_ServerValidate">
                  </asp:CustomValidator>

 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
            {
            if (FileUpload1.HasFile)
                {
                System.IO.FileStream fstre = new System.IO.FileStream(FileUpload1.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                if (fstre.Length > 1024 * 1)//Just checking to see if file is larger than 1kb
                    {
                    args.IsValid = false;
                    CustomValidator1.Text = "File must be less than 200kb's";
                    CustomValidator1.Visible = true;
                    }
                else
                    {
                    args.IsValid = true;
                    }
                }

            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros