2017-03-21 55 views
0

我试图上传一个文件,当点击一个按钮时。问题是在代码Uncaught TypeError:无法读取未定义文件上传的属性“长度”Dropzone JS

Dropzone.options.myDropzone = { 

    autoProcessQueue: false, 

    init: function() { 
     var submitButton = document.querySelector("#submit-all") 
     myDropzone = this; 

     submitButton.addEventListener("click", function() { 
      myDropzone.processQueue(); 
     }); 


     this.on("addedfile", function() { 

     }); 

    } 
}; 

以上第二块是我创建一个新的悬浮窗部分,楼下是我logic.I在我的for循环得到一个错误。说长度是不确定的。

$("#submit-all").click(function (e) { 
    $('#myPleaseWait').modal('show') 
    var fileUpload = $("#files").get(0); 
    var files = this.file; 
    var data = new FormData(); 
    var _url = $(this).data('appcontroller'); 
    for (var i = 0; i < files.length ; i++) { //Error is stopping here when upload button is pressed 
     data.append(files[i].name, files[i]); 
    } 
    $.ajax({ 
     type: "POST", 
     url: _url, 
     contentType: false, 
     processData: false, 
     data: data, 
     success: function (message) { 
      $('#myPleaseWait').modal('hide'); 
      alert(message); 
     }, 
     error: function() { 
      $('#myPleaseWait').modal('hide'); 
      alert("There was error uploading files!"); 
     } 
    }); 
}); 

我添加了一个JS拨弄我的HTML代码https://jsfiddle.net/q47axhdf/2/

任何帮助将不胜感激

+0

'var files = fileUpload.file;'应该是'var files = fileUpload.files;' –

+0

hi rory,忘记把它改成this.files,这就是为什么我得到我的长度undefined错误消息 –

+0

所以你是说现在它已经修好了吗? –

回答

0

像往常一样找到了答案张贴此之后。我在for循环中从我的file.length中删除了.length,并且上传完美。

相关问题