实际上我尝试使用这个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>
答案 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插件的一部分,没有名为fileType
,maxFileSize
和minFileSize
的规则。您需要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
}
});