我有一個代碼,適用於通過Ajax與FormData和XMLHttpRequest發送多個文件;如何知道所有上傳是在循環內完成的?
for (var i=0, j=this.files.length; i<j; i++) {
file = this.files[i];
var formdata = new FormData();
formdata.append("images[]", file);
var xhr = new XMLHttpRequest(),
upload = xhr.upload,
id = Math.floor((Math.random() * 100000));
upload.addEventListener("loadstart", function(e){
showUploadedItem(file, this.id);
});
upload.id = id;
upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
)
};
upload.onload = function(e) {
if (this.status == 200) {
console.log('');
}
};
xhr.onreadystatechange = function(e) {
if (4 == this.readyState) {
console.log('');
}
};
xhr.open('post', '<?php echo Yii::app()->createUrl('url') ?>', true);
xhr.send(formdata);
}
我發送每個文件作爲環內一個新的XMLHttpRequest對象,所以當我收到結尾的請求,我不知道。
任何人都可以幫忙嗎?
你可能甚至不需要setTimeout。 – jwebb