Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Custom validator on file size

Posted on 2007-07-23
5
Medium Priority
?
561 Views
Last Modified: 2013-11-07
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;
                    }
                }

            }
0
Comment
Question by:LearningCSharpSQL
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:SimonFerris
ID: 19545929
Check that you have AutoWireUpEvents set to true or if this is not an option write the event handler. In C# the AutoWireUpEvents is set to true by default.
0
 

Author Comment

by:LearningCSharpSQL
ID: 19546085
Hi SimonFerris:
I have but no luck
Regards
George
0
 
LVL 2

Accepted Solution

by:
SimonFerris earned 2000 total points
ID: 19546171
Here is a link to an MSDN article about FileUpload validation.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

It looks like that the validation you are trying to perform cannot be done until you obtain a handle to the file. The initial control only allows you to specify the path.
0
 
LVL 2

Expert Comment

by:SimonFerris
ID: 19546223
0
 

Author Comment

by:LearningCSharpSQL
ID: 19575679
Thanks Simon
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question