nodejs multer jquery步骤向导和引导文件上载器

问题描述:

我在上载jQuery向导中的多个文件时遇到问题。 该向导包含多个正在发送到服务器(nodejs)的字段,但似乎无论我做什么,这些文件都是未定义的。nodejs multer jquery步骤向导和引导文件上载器

下面是一些代码片段:

<form id="wodcreate" class="steps-validation" action="#" enctype="multipart/form-data"> 

...

<div class="form-group"> 
    <div class="col-lg-10"> 
     <input type="file" id="images" name="images" class="file-input" accept="image/*" multiple="multiple"> 
     <span class="help-block" data-i18n="general.Only-images-allowed">Only images are allowed. Maximum size of each image is 5 MB, and there is a maximum of 10 images.</span> 
    </div> 
</div> 

...

app.post('/api/user/create', upload.any(), function (req, res, next) { 
    //Pass the logic to a dedicated function 
    console.log(req.body); 
    console.log(req.files); 
    res.json({success : "Updated Successfully", status : 200}); 
}); 

发送到服务器,当我用FORMDATA,因为我使用一个向导,我不使用jquery上传按钮,但用这种方法将图像添加到formdata:

$('#images').on('filebatchpreupload', function(event, data, previewId, index) { 
var form = data.form, files = data.files, extra = data.extra, 
    response = data.response, reader = data.reader; 

$.each(files, function (key, value) { 
    if(value != null){ 
     formData.append("images", value, value.name); 
    } 
}); 

在向导的onFinished功能如下:

onFinished: function (event, currentIndex) { 

    var res = $(".steps-validation").valid(); 
    var model_data = $(".steps-validation").serializeArray(); 

    //For all normal input fields 
    $.each(model_data,function(key,input){ 
     formData.append(input.name,input.value); 
    }); 
    //For videolist (li nonform fields): 
    $('#videolist li').each(function(){ 
     formData.append('videolist[]',$(this).attr("id")); 
    }); 

    //For wysiwyg 
    var desc = CKEDITOR.instances.desc.getData(); 
    formData.append('desc',desc); 

    if (res == true) { 
     jQuery.ajax({ 
      type: "POST", 
      url: "/api/user/create", 
      data: formData, 
      processData: false, // tell jQuery not to process the data 
      contentType: false, // tell jQuery not to set contentType 
      success: function (data) { 
       if(data.status == 200){ 

       } 
      } 
     }); 
    } 
    return res; 
} 

一段时间后,我终于想通了,错误的功能,是由于:

$('#images').on('filebatchpreupload') 

没有被触发。