在for循环中的Ajax请求中进行多次上传

时间:2017-02-01 09:13:57

标签: javascript jquery ajax

我只在上传1个文件时在ajax for循环中遇到问题,成功只上传了1个文件..但是当我上传2个文件时,上传的响应变为4 ..当我上传3个得到2倍......

     for (var i = 0, file_name; file_name = dropzone.files[i]; i++) {

         $.ajax({
//THIS function ofr the progress of ajax call
          xhr: function() {
          var xhr = new window.XMLHttpRequest();
          xhr.upload.addEventListener("progress", function(evt) {

            if (evt.lengthComputable) {

              var percentComplete = evt.loaded / evt.total;
              percentComplete = parseInt(percentComplete * 100);

                      if (percentComplete === 100) {
                                $( "#spinner" ).hide();

                }

                else if (percentComplete !== 100){

               $( "#spinner" ).show();
                }

              }
            }, false);

            return xhr;


          },

           url: url,
           type: 'POST',
           data: file_name,
           processData: false,
           contentType: 'application/octet-stream',
           headers: {
           "Authorization": "Bearer ACCESSTOKEN",
           "Dropbox-API-Arg": '{"path": "/'+file_name.name+'", "mode": "add","autorename": true,"mute": false}'
           },



           success: function (data , response){

               console.log(data);

           },


          })




       }

0 个答案:

没有答案