2013-02-12 34 views
0

所以我一直在努力獲得一個拖放文件上傳腳本,它爲一個單一的文件。我現在試着讓它允許多個文件,但是遇到了問題。調整單個文件上傳器成爲多文件上傳器

// Required for drag and drop file access 
jQuery.event.props.push('dataTransfer'); 

// Set the dropzone 
$("#dropzone-wrapper").on('drop', function(event) { 
// Do something with the file(s) 
var files = event.dataTransfer.files; 

if (files.type.match('image.*')) { 
    // If the file is an image, then run the processFileUpload function 
    processFileUpload(files); 
} else { 
    alert("Not an image!"); 
} 
}); 

// Process the file that was dropped 
function processFileUpload(droppedFile) { 

var uploadFormData = new FormData(); 
uploadFormData.append("image",droppedFile); 

var bar = $('.bar'); 
var percent = $('.percent'); 

$.ajax({ 
    url : "Uploader/upload.php", // use your target 
    type : "POST", 
    beforeSend: function() { 
     $('.progress').show(); 
     var percentVal = 0; 
     bar.width(percentVal) 
     percent.html(percentVal + '%'); 

     window.setInterval(function(){ 
      var x = (100/(droppedFile.size/100000)); 
      var x = Math.round(x); 

      if (x >= 90) { clearInterval(); } else { 
      percentVal = percentVal + x; 
      percent.html(percentVal + '%'); 
      bar.width(percentVal + '%'); } 
     }, 1000); 

    }, 
    complete: function() { 
     clearInterval(); 
     bar.width("100%"); 
     percent.html("100%"); 
    }, 
    data : uploadFormData, 
    chache : false, 
    contentType : false, 
    processData : false, 
    success : function(data) { 

     window.location = data; 

    } 
    }); 
} 

我得到的錯誤是* 無法調用未定義的「匹配」 *它涉及到線路9。現在我的猜測是,它是有一個問題檢查多個文件,因爲正如我所說,檢查一下就可以了。我如何着手檢查上傳的所有文件是否爲圖像?

回答

1

您必須添加單獨的每個文件,files是文件列表,你也必須改變processFileUpload處理多個文件

var filesToUpload = []; 
for (var i = 0; i < files.length; i++){ 
    if (files[i].type.match('image.*')) { 
     filesToUpload.push(files[i]); 
    } else { 
     alert("Not an image!"); 
    } 
} 
processFileUpload(filesToUpload); 

... 

function processFileUpload(droppedFiles) { 
... 
var uploadFormData = new FormData(); 
for (var i = 0; i < droppedFiles.length; i++){ 
    uploadFormData.append("image[]",droppedFiles[i]); 
} 
... 
+0

用現在這樣做的唯一的問題...它不發送所有的圖像一次到我的upload.php,所以我不能把它們當作一個數組。它分別發送每個文件。這是應該發生的嗎? – Tenatious 2013-02-12 21:44:27

+0

@Tenatious我認爲它會將它們作爲一個數組發送出去,我會測試它並回復給你。 – Musa 2013-02-12 21:46:16

+0

@Tenatious我把它作爲一個數組在PHP中,你在服務器端使用什麼? – Musa 2013-02-12 21:54:27