Implement File Size Restriction Validation using jQuery
 
Author:
Filed Under: ASP.Net  |  JavaScript  |  JQuery  |  jQuery Plugins
Published Date: Jul 08, 2011
Views: 3252
 

Abstract: Here Mudassar Ahmed Khan has explained how to implement File size limit validation before upload using jQuery uploadify plugin

Comments:  2

 

In one of my previous articles I have explained how to upload multiple files GMAIL style using jQuery Uploadify plugin
Here I will explain how we can use restrict the file size before upload using this plugin. jQuery Uploadify plugin has a sizeLimit attribute which accepts the size in bytes.
 
<script type = "text/javascript">
    $(window).load(
    function () {
        $("#<%=FileUpload1.ClientID %>").fileUpload({
            'uploader': 'scripts/uploader.swf',
            'cancelImg': 'images/cancel.png',
            'buttonText': 'Browse Files',
            'script': 'Upload.ashx',
            'folder': 'uploads',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            'multi': true,
            'auto': true,
            'sizeLimit': (600 * 1024), //600 KB
            onError: function (a, b, c, d) {
                if (d.type === "File Size") {
                    // Display error message in span
                    $("#err").html('File: ' + c.name + ' Maximum ' + d.type + ' Limit: ' + Math.round(d.sizeLimit / 1024) + 'KB');
                }
            }
        });
    }
);
</script>
 
As you can see above I have specified the size limit of 600 KB. If the file size exceeds the limit it throws File Size error as shown in the screenshot below.
Autocomplete with images jQuery plugin - Autocomplete list with images
Downloads
You can download the sample code in VB.Net and C# using the download link provided below.
File Size Validation using jQuery








Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit