我打算使用jQuery上傳一組文件。如何在一個函數中順序發送文件上傳,這也是一個延期承諾?
這個意圖被包裹在一個叫做uploadFilesUsingAjax()
的函數中;
var uploadFilesPromise = uploadFilesUsingAjax();
$.when(uploadFilesPromise).done(function (uploadFilesAjaxResult) {
// redirect to success page...
我需要等待所有的文件成功上傳,然後再做其他事情。
裏面uploadFilesUsingAjax()
,
我寫我的代碼,這樣
function uploadFilesUsingAjax() {
var files = pages; // pages is a global variable which is an array of files
var url = "https://stackoverflow.com/users/" + currentUser.id + "/files.json";
var type = "POST";
console.info('files length:' + files.length);
if (files.length > 0) {
var promises=[];
for (var i = 0; i < files.length; i++) {
var data = new FormData();
var postData = {};
var file = files.getByIndex(i);
var key = i + 1;
if (typeof (file.id) !== "undefined" && file.id > 0) {
data.append(key, JSON.stringify(file));
} else {
data.append(key, file);
}
var request = $.ajax({
//this is the php file that processes the data
url: url,
//POST method is used
type: type,
//pass the data
data: data,
//Do not cache the page
cache: false,
xhr: function() {
// custom xhr
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload) { // check if upload property exists
myXhr.upload.addEventListener('progress',updatePagesProgress, false); // for handling the progress of the upload
}
return myXhr;
},
// DO NOT set the contentType and processData
// see http://stackoverflow.com/a/5976031/80353
contentType: false,
processData: false,
//success
success: function (json) {
// json is already an object thanks to cakephp code to serialize
//if POST is a success expect no errors
if (json.error == null && json.result != null) {
currentUser = json.result.User;
// error
} else {
alert(json.error);
}
}
});
promises.push(request);
}
var promise = promises[0];
for (var i = 1; i < promises.length; i++) {
promise = promise.then(promises[i]);
}
return promise.done(function() { console.log('all!')});
不幸的是,我沒能上傳大量的文件之前,我得到重定向到成功頁面。
我已經嘗試過關於如何做到這一點的各種StackOverflow解決方案。到目前爲止沒有任何工作請指教。
某些代碼已被截斷以節省空間。
我必須使用'.map'一個小問題,因爲URL可能取決於我想使用uploadFilesUsingAjax()是不同的。我如何克服這種情況? –
您正在將_stuff_數組映射到請求任務數組。您可以訪問文件對象 - 根據傳遞給''.map'函數的'file'對象的屬性,您可以將'.map'轉換爲不同的URL。 –
對此感到抱歉。我知道如何解決這個問題。我改寫了別名函數。我曾嘗試在答案中添加一個額外的括號。 BUt不能,因爲StackOverflow要求添加至少6個字符。你錯過了'd.reject(新錯誤(「被調用的uploadFiles沒有要上傳的文件」));'最後應該是雙括號。 –