How to check the size of an uploaded file using Jquery and Jquery form validation plugin?

限于喜欢 提交于 2019-11-28 13:12:55
Svetlozar Angelov

You can't do it with JQuery. The problem is that javascript can not access the file system.

Here is something you can read about the subject - Using jQuery, Restricting File Size Before Uploading

Jquery validation plugin code

 $.validator.addMethod('filesize', function (value, element, arg) {
            var minsize=1000; // min 1kb
            if((value>minsize)&&(value<=arg)){
                return true;
            }else{
                return false;
            }
        });

        $("#myform" ).validate({
            rules: {
                file_upload:{
                    required:true,
                    accept:"application/pdf,image/jpeg,image/png",
                    filesize: 200000   //max size 200 kb
                }
            },messages: {
                file_upload:{
                    filesize:" file size must be less than 200 KB.",
                    accept:"Please upload .jpg or .png or .pdf file of notice.",
                    required:"Please upload file."
                }
            },
            submitHandler: function(form) {
                form.submit();
            }
        });

Min 1 kb to max 200 kb file size and it is tested

And a mere seven years later... I have a pull request in to validate the max size of a file (or a collection of files). It's under code review right now. See https://github.com/jquery-validation/jquery-validation/pull/2087

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!