jQuery 验证插件:验证多个输入文件

jQuery validation-plugin: validating multiple input files

我想问一下如何使用 jQuery 验证插件验证多个文件输入。

目前我有这些代码,但它不起作用:

.html:

<form id="uploadPhotoForm" enctype="multipart/form-data" method="POST">
    <table class= "uploadPhotoTable">
        <tr>
            <td>Photo</td>
            <td>:</td>
            <td><input class="field" type="file" name="files[]" id="upload_photo" align='right' multiple /></td>
        </tr>
    </table>    
</form>

.js:

$('#uploadPhotoForm').validate({
    rules: {
      files: {
      required: true,
      extension: "png"
    }
    },
    messages:{
        files:{
           required : "Please upload atleast 1 photo",
           extension:"Only png file is allowed!"
        }       
    }
  });

我也将使用这段代码对post到新的PHP进行处理。但在我的 uploadPhoto.php 中,$_FILES['files']['tmp_name'] 似乎是未定义的。我可以知道如何解决这个问题吗?

if ($('#uploadPhotoForm').valid()) {       
    $.ajax({
        url: "inc/uploadPhoto.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(data){
           $("#error1").html(data);
        }           
    });
}

你需要'files[]'而不是files,如果你不添加additional-methods.js,你会这样做。

  $('#uploadPhotoForm').validate({
    rules: {
      'files[]': {
      required: true,
      extension: "png"
    }
    },
    messages:{
        'files[]':{
           required : "Please upload atleast 1 photo",
           extension:"Only png file is allowed!"
        }

    }

参见 jsFiddle

关于连载。 请阅读此 how to do file upload using jquery serialization

更新:

它会起作用

if ($('#uploadPhotoForm').valid()) {  
    var form = $('#uploadPhotoForm')[0]; //  [0], because you need to use standart javascript object here
    var formData = new FormData(form);
    $.ajax({
        url: "inc/uploadPhoto.php",
        type: "POST",
        data:  formData,
        contentType: false,
        cache: false,
        processData:false,
        success: function(data){
           $("#error1").html(data);
        }           
    });
}

我认为您的代码无效,因为 data: new FormData(this), 中的 this 无效 javascript 表单对象。

对于某些可能通过 google.I 登陆此页面的人,使用以下解决方案解决了这个问题,因为它无法正常处理多个不同格式的上传。

<input type="file" id="upload_files" name="uploaded_files[]"  required="required" multiple>

我创建了自定义验证器方法,因为默认验证器未验证多个文件 properly.If 我 select 第一个文件为 pdf,另一个 png.This 示例验证 pdf、doc 和 docx文件。

$(function() {
    $.validator.addMethod(
            "validate_file_type",
            function(val,elem) {
                console.log(elem.id);
                    var files    =   $('#'+elem.id)[0].files;
                console.log(files);
                for(var i=0;i<files.length;i++){
                    var fname = files[i].name.toLowerCase();
                    var re = /(\.pdf|\.docx|\.doc)$/i;
                    if(!re.exec(fname))
                    {
                        console.log("File extension not supported!");
                        return false;
                    }
                }
                return true;
            },
            "Please upload pdf or doc or docx files"
    );
    // Initialize form validation on the registration form.
    // It has the name attribute "registration"
    $("form[name='formname']").validate({
        // Specify validation rules
        rules: {
            'uploaded_files[]':{
                required: true,
                validate_file_type : true
            }
        },
        // Specify validation error messages
        messages: {

            'uploaded_files[]':{
                required : "Please upload atleast 1 document",
                /*accept:"Please upload .docx or .pdf files"*/
            }
        },
        // Make sure the form is submitted to the destination defined
        // in the "action" attribute of the form when valid
        submitHandler: function(form) {
            form.submit();
        }
    });

});