JQuery:如何使用dropzone.js将多个文件放入formData并通过ajax发送

时间:2016-09-28 16:24:52

标签: javascript jquery ajax dropzone.js

我在尝试上传多个文件时遇到问题。我有两种方法通过ajax发送到页面。一个是dropzone,另一个是我的XMLHttpRequest。每次我通过点击按钮发送它只复制我的请求,只有第一种方法是dropzone并忽略我的XMLHttpRequest。所以我正在尝试弄清楚将它绑定到formData的方式,使它只能一起请求文件。

我的表格

        <form style="border:3px dashed #D9EDF7;" action="UploadImage"
              class="dropzone dz-clickable"
              id="my-awesome-dropzone" enctype="multipart/form-data">
              <div class="dz-message">Drop files here or click to upload.
                        <br> <span class="note">(Selected files are not 
              automatically uploaded.)</span>
                    </div>
            <div class="fallback">
                <input name="file" id="filez" type="file" multiple/>
             </div>

        </form>

我的Js

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone('#my-awesome-dropzone', {
   url : '../ajax/ajax_add/ajax_addNEWContestant.php?
   multipleImage=multiple_image&event_id='+event_id,
   autoProcessQueue : false
});

$(document).on('click','#addnewContestant', function(e){
    myDropzone.processQueue();

    var formdata = new FormData();
    formdata.append('file_addnew', file_addnew.files[0]);
    //If there is a way, I just want to bind all the files from dropzone into formData
    var data = new FormData();
    for(var b=0; b<imageContainer.length; b++){
        formdata.append('All_files_from_dropzone[], input_file.files[b]);
    }



   var param = "?event_id="+encodeURIComponent(event_id)+
            "&contestant_name="+encodeURIComponent(contestant_name)+
            "&contestant_lastName="+encodeURIComponent(contestant_lastName)+
            "&conAge="+encodeURIComponent(conAge)+
            "&hAddress="+encodeURIComponent(hAddress)+
            "&email_add="+encodeURIComponent(email_add)+
            "&conContactNum="+encodeURIComponent(conContactNum)+
            "&conDesc="+encodeURIComponent(conDesc)+
            "&conId_hidden="+encodeURIComponent(conId_hidden)+
            "&hidden_gender="+encodeURIComponent(hidden_gender)+
            "&random_gender="+encodeURIComponent(random_gender)+
            "&multipleImage="+encodeURIComponent(multipleImage);


   beforeSend();            
   xhr = new XMLHttpRequest();
   var url = '../ajax/ajax_add/ajax_addNEWContestant.php';
   xhr.open('post', url+param, true);

});

1 个答案:

答案 0 :(得分:0)

Dropzone已经提交了其他表单字段以及文件,因此您无需自己进行任何xhr工作