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

JQuery : How to put multiple files into formData using dropzone.js and send it via ajax

本文关键字:formData 发送 ajax 文件 何使用 dropzone js JQuery      更新时间:2023-09-26

我有一个问题试图上传我的多个文件。我有两个方法通过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);
});

Dropzone已经将其他表单字段与文件一起提交,因此您不需要自己做任何xhr工作