2014-02-18 26 views
1

只有在處理了所有文件後,纔會在blueimp中發送成功或錯誤消息...否則,第一個文件將發送成功消息並如果另一個文件有錯誤,它也會觸發錯誤。只有在處理完所有文件後才發送錯誤消息或成功消息

這裏是我的代碼:

// Initialize the jQuery File Upload widget: 
jQuery('#fileupload').fileupload({ 
    url: url, 
    done: function (e, data) { 
     var response = jQuery.parseJSON(data.jqXHR.responseText); 
     var error = response.files[0].error; 

     if(error) { 
      jQuery('#error').show(); 
      jQuery('#error').append('<p><strong>Error given:</strong> '+error+'</p>'); 
     } else { 
      jQuery('#success').show(); 
     } 
    }, 
}); 

感謝您的幫助!

回答

1

當詢問在github塞巴斯蒂安Tschan,blueimp jQuery的文件上傳的創造者的幫助,建議我嘗試使用fileuploadstop方法,而不是singleFileUploads ......然而,沒有任何的blueimp stop方法有data參數,下面是我如何能夠解決這個問題(感謝開發者的朋友對他的幫助):

Javascript:

// Using the Basic Plus UI version of the plugin ... 

// Global variables 
var status = new Array(), // Create a new array 
    successAll = true; // Used to check for successful upload 

// After you've initialized the jQuery File Upload widget: 
jQuery('#fileupload') // Replace with your form id 
    .bind('fileuploaddone', function (e, data) { 
     // Append the file response data to the global array, in my case "status" 
     status.push(jQuery.parseJSON(data.jqXHR.responseText)); 
    }) 
    .bind('fileuploadstop', function (e) { 
     for (var i = 0; i < status.length; i++) { 
      var error = status[i].files[0].error; 

      if(error) { 
       jQuery('#error').show(); 
       jQuery('#error').append('<p><em>Error given: '+error+'</em></p>'); 

       successAll = false; // Change success value to false if error 
      } 
     } 

     // If successAll is true, meaning it wasn't reset because of an error, 
     // display success message. 
     if (successAll) { 
      jQuery('#success').show(); 
     } 
     status = new Array();  
    }); 

很好用!希望它能幫助別人。

+0

感謝它幫了我很多.. –

+0

這個腳本有點不穩定。它不適用於autoUpload設置爲true,並且在點擊每個上傳中的「開始上傳」按鈕時不起作用。它只能從主要的「開始上傳」按鈕上下載一個關閉的所有文件 – Saymon

2

SingleFileUploads選項

如果你希望你的文件上傳單個請求,您可以使用following optionsingleFileUploads:false,

這樣,只有1 回調將觸發對每個文件。

停止回調

創建一個全局數組:

var temp = new Array(); 

然後在你的失敗,或總是回調應該填充文件的名稱和成功地位的陣列:

done: function(e, data) { 
    $.each(data.files, function(i, f) { 
     temp.push({"name": f.name, "succeed": data.textStatus}) 
    }) 
}, 

最後,你可以檢查每更迭身份進入停止回調被稱爲當每個文件已經完成:

stop: function(e, data) { 
    $.each(temp, function(i, f) { 
     console.log(f.name) 
     console.log(f.succeed) 
    }); 
    // Don't forget to empty your array to allow other downoal 
    temp= new Array(); 
}, 
+0

甜!那解決了這個問題......但是,它也創造了一個。現在我的模板只在第一個文件上顯示一次取消按鈕。我該如何解決?如果您需要更多信息,請告訴我。 –

+0

@ Designer17這取決於您是如何顯示這些取消按鈕的? – Fractaliste

+0

這是用戶界面的UI:[UI截圖](http://imgur.com/iL4vWNJ); 這裏是我使用的'template'代碼:[upload template](http://pastebin.com/BZ0f7nhP)。 希望這是你所要求的。 –

相關問題