Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Tip/Trick

Asp.Net Custom Validator Control to validate file size

Rate me:
Please Sign up or sign in to vote.
3.42/5 (4 votes)
25 Nov 2011CPOL 68.9K   5   5
Asp.Net Custom Validator Control to validate file size both client side validation and server side validation
The following code is used to validate the file size before uploading the file.
We use the asp.net custom validation control to achieve this. We can use asp.net
custom validation control to validate either client side or at server side. We see
both the things for this purpose.

Asp.Net Custom Validation Control Client Side Validation to Validate File Size:
 <script type ="text/javascript">

    function checkfilesize(source, arguments) {
        arguments.IsValid = false; 

        var axo = new ActiveXObject("Scripting.FileSystemObject");
        thefile = axo.getFile(arguments.Value);
        var size = thefile.size;
        if (size > 5000) {
            arguments.IsValid = false;
        }
        else {
            arguments.IsValid = true;
        }


    }
</script>
    <asp:FileUpload ID="FileUpload1" runat="server" /><br />
    <asp:Button ID="Button1" runat="server" Text="Upload"/>
    <br />
    <asp:CustomValidator ID="NewPasswordCustomValidator" runat="server"  
                    Text="*" ToolTip="FileSize should not exceed 5kb" 
                    ErrorMessage="FileSize Exceeds the Limits.Please Try uploading smaller size files." 
                    ControlToValidate="FileUpload1"  
                   ClientValidationFunction = "checkfilesize">
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

Asp.Net Custom Validation Control Server Side Validation to Validate File Size:
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
  <asp:Button ID="Button1" runat="server" Text="Upload"/>
  <br />
  <asp:CustomValidator ID="NewPasswordCustomValidator" runat="server"
                  Text="*" ToolTip="FileSize should not exceed 5kb"
                  ErrorMessage="FileSize Exceeds the Limits.Please Try uploading smaller size files."
                  ControlToValidate="FileUpload1"
                  OnServerValidate = "checkfilesize">
  <asp:ValidationSummary ID="ValidationSummary1" runat="server" />


The C# function to validate:
protected void checkfilesize(object source, ServerValidateEventArgs args)
    {
        string data = args.Value;
        args.IsValid = false;
        double filesize = FileUpload1.FileContent.Length;
        if (filesize > 5000)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

I hope this simple technique would help you to perform your custom validations



Understanding The Code:
The code describe is very simple and helps you achieve the task. Initially start
by adding the asp.net custom server control and then define the following properties to it:
1. Text: The text to be displayed.
2. Error Message: Error message for the user.
3. ControlToValidate: Validation control on which the validation should be performed.
and finally we have two things

4.OnServerValidate : the name of function if you want to validate by using your server side langauge.
(or)
ClientValidationFunction : the name of JavaScript function if you want to validate the client side.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Collabera
Singapore Singapore
S V Sai Chandra is a Software Engineer from Hyderabad Deccan. He started Embedded Programing in his college days and now he is a Web Developer by Profession. He Loves coding and his passion has always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books, painting and hang out with friends is his most favorite past time hobby.
He blogs at
http://technowallet.blogspot.com
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.

Comments and Discussions

 
QuestionError page Pin
133bharti10-May-20 21:23
133bharti10-May-20 21:23 
QuestionNot working why? its so Pin
Ajay kumar sahu23-Nov-14 20:45
Ajay kumar sahu23-Nov-14 20:45 
AnswerRe: Not working why? its so Pin
Alejandro Del Genio8-Sep-15 7:02
Alejandro Del Genio8-Sep-15 7:02 
Questionwhat if i want the validation to occur before i press the upload button Pin
pree_kh29-Nov-12 22:34
pree_kh29-Nov-12 22:34 
GeneralYour client-side validation code will only work in Internet ... Pin
Richard Deeming1-Dec-11 4:44
mveRichard Deeming1-Dec-11 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.