输入文件类型图像大小限制不起作用

时间:2015-01-22 14:47:35

标签: jquery file-upload jquery-validate

实际上我尝试使用这个url进行jquery图像文件验证,但它没有工作。请你验证并告诉我吗?


点击此处:https://github.com/snyderp/jquery.validate.file

<form method="post" enctype="multipart/form-data" id="upload_form">
    <input type="file" name="example_file" name="example_file">
    <button type="submit">Upload</button>
</form>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script>
$(function () {
    $("#upload_form")
        .validate({
            rules: {
                example_file: {
                    fileType: {
                        types: ["text", "gzip", "zip"]
                    },
                maxFileSize: {
                    "unit": "KB",
                    "size": 100
                },
                minFileSize: {
                    "unit": "KB",
                    "size": "10"
                }
            }
        });
});
</script>

2 个答案:

答案 0 :(得分:0)

你想使用扩展名,所以只包括jquery和jquery.validate是不够的。所以还要通过下载插件来包含插件。 (我知道这个例子中缺少它)

最终解决方案应该是:

<form method="post" enctype="multipart/form-data" id="upload_form">
    <input type="file" name="example_file" name="example_file">
    <button type="submit">Upload</button>
</form>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="jquery.validate.file.js"></script>
<script> $(function () {
    $("#upload_form")
        .validate({
            rules: {
                example_file: {
                    fileType: {
                        types: ["text", "gzip", "zip"]
                    },
                maxFileSize: {
                    "unit": "KB",
                    "size": 100
                },
                minFileSize: {
                    "unit": "KB",
                    "size": "10"
                }
            }
        });
    });
</script>

编辑:在jsfiddle中测试uploadfunction不是最好的主意,因为它一般不允许文件上传。

答案 1 :(得分:0)

作为jQuery Validate插件的一部分,没有名为fileTypemaxFileSizeminFileSize的规则。您需要write your own rules you simply forgot to include the custom file uploading rules from GitHub that you referenced

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<!--// need to include this one too //-->
<script src="jquery.validate.file.js"></script>

你也有一个缺席的大括号...

$("#upload_form").validate({
    rules: {
        example_file: {
            fileType: { 
                types: ["text", "gzip", "zip"]
            },
            maxFileSize: { 
                "unit": "KB",
                "size": 100
            },
            minFileSize: { 
                "unit": "KB",
                "size": "10"
            }
        } // <- THIS one was missing
    }
});
  1. 包含添加文件上传方法的插件。
  2. 修复缺失的大括号。
  3. 工作演示:http://jsfiddle.net/h2nduLco/3/