简体   繁体   中英

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

I have a problem trying to upload my multiple files. I have two method sending into a page via ajax. One is the dropzone, and other is my XMLHttpRequest . Every time I send by clicking button it duplicates my request following only the first method which is dropzone and ignoring my XMLHttpRequest . So I am trying figure out the way to send it binding into a formData to make it only 1 request of files all together.

My form

        <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>

My 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工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM