Validate File Size and Extension of File to be uploaded using jQuery or JavaScript

Last Reply on Jun 10, 2014 02:21 AM By Azim

Posted on Jun 10, 2014 02:18 AM

w to find the file size and file extension of th uploded file in javascript or jquery

Posted on Jun 10, 2014 02:21 AM

Reference:

 
For extension validation you can see this line in the above article. Add your valid extension.
'fileExt': '*.jpg;*.jpeg;*.gif;*.png',

 For Size limit. You can also change the size.

'sizeLimit': (600 * 1024), //600 KB

Complete Script

<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>