我有一个上传表单,在使用AJAX上传文件时显示状态栏。当上传完成后,我希望用户知道但是当它完成时没有任何反应,但我告诉它警告“完成”。
我的代码:
UploadB.click(function(event)
{
event.preventDefault();
event.stopPropagation();
var FInput = $('#Files').val();
if(FInput != '')
{
var Data = new FormData();
var Files = document.getElementById('Files');
for(var I = 0; I < Files.files.length; ++I)
{
var FilesName = Files.files[I].name;
Data.append('File[]', Files.files[I]);
}
var Request = new XMLHttpRequest();
Request.upload.addEventListener('progress', function(event)
{
if(event.lengthComputable)
{
Percent = event.loaded / event.total;
Progress = document.getElementById('Progress');
Loaded = Math.round(Percent * 100);
$('#Progress').progressbar({
value: Loaded
});
}
else
{
$('#Progress').text('There Was An Error Getting The Percent');
}
});
Request.upload.addEventListener('load', function(event)
{
});
Request.upload.addEventListener('error', function(event)
{
alert('Upload Failed.');
});
Request.upload.addEventListener('readystatechange', function(event)
{
if(this.readyState == 4)
{
if(this.status == 200)
{
alert('Done.');
}
else
{
alert('There Was An Error, Either Try Again Or Contact Us');
}
}
});
Request.open('POST', 'Upload.php');
Request.setRequestHeader('Cache-Control', 'no-cache');
Progress.style.display = 'block';
Request.send(Data);
}
else
{
alert('Please Select A File / Files');
}
});
答案 0 :(得分:1)
据我所知,readystatechange
事件仅在XMLHttpRequest
上触发,而不在XMLHttpRequest.upload
上触发。
尝试:
Request.addEventListener('readystatechange', function(event) {
if(this.readyState == 4)
{
if(this.status == 200)
{
alert('Done.');
}
else
{
alert('There Was An Error, Either Try Again Or Contact Us');
}
}
});