Click here to Skip to main content
11,704,107 members (67,531 online)
Click here to Skip to main content

Asp.Net Custom Validator Control to validate file size

, 25 Nov 2011 CPOL 36.9K 5
Rate this:
Please Sign up or sign in to vote.
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" />
 
    <asp:Button ID="Button1" runat="server" Text="Upload"/>
    
 
    <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" />
 
  <asp:Button ID="Button1" runat="server" Text="Upload"/>
  
 
  <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)

Share

About the Author

S V Saichandra
Software Developer Emirates Group IT
United Arab Emirates United Arab Emirates
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.

You may also be interested in...

Comments and Discussions

 
QuestionNot working why? its so Pin
Ajay kumar sahu23-Nov-14 20:45
memberAjay kumar sahu23-Nov-14 20:45 
Questionwhat if i want the validation to occur before i press the upload button Pin
pree_kh29-Nov-12 22:34
memberpree_kh29-Nov-12 22:34 
GeneralYour client-side validation code will only work in Internet ... Pin
Richard Deeming1-Dec-11 4:44
memberRichard Deeming1-Dec-11 4:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 25 Nov 2011
Article Copyright 2011 by S V Saichandra
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid