XMLHttpRequest Level 2 - Determinate if upload finished(XMLHttpRequest Level 2 - 确定上传是否完成)
问题描述
我正在使用 ajax 进行文件上传.文件上传后,php 应该检查它(mime、大小、病毒(clamscan)等等)——对于较大的文件,这需要几秒钟.当文件上传时,一个 HTML5 <progress>
正在填充,当文件准备好并且 PHP 开始检查时,进度应该切换到不确定.我想到了这样做的方法(两者都不工作):
I am using ajax for file uploads.
After the file is uploaded, php should check it (mime, size, virus (clamscan) and more) - this takes some seconds for larger files. While the file is uploading, a HTML5 <progress>
is filling, when the file is ready and PHP starts checking, the progress should switch to indeterminate. I thought of to ways to do this (which both do not work):
检查 upload.onload 事件
xhr.upload.addEventListener("load", function (e) {
$("#uploadprogress").attr("value", false);
$("#uploadprogress").attr("max", false);
$("#progress").text("Checking file...");
});
这不起作用,因为 onload
-事件在请求准备好时触发,而不是在上传准备好时触发.
This doesn't work, because the onload
-event firest when the request is ready, not when upload is ready.
检查上传进度百分比是否 = 100%
xhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable && e) {
p = (e.loaded / e.total);
if (p==1) {
$("#uploadprogress").attr("value", false);
$("#uploadprogress").attr("max", false);
$("#progress").text("Checking file...");
} else {
var percent = Math.ceil(p * 1000) / 10;
$("#uploadprogress").val(e.loaded);
$("#uploadprogress").attr("max", e.total);
$("#progress").text("Uploading... " + percent + "%");
}
}
}
});
这不起作用,因为上传百分比有时会停止在大约.97%,尽管上传完成并且 PHP 开始处理文件
This does not work, because the upload percentage sometimes stops at approx. 97%, despite the upload is finished and PHP starts handling the files
还有其他可能检查这个吗?
Is there another possibility checking this?
推荐答案
您要监听的事件是 XHR 对象上的 readystatechange
(不是 XHR.upload 上的).readyState
是 4
当上传完成发送并且 服务器关闭连接.loadend
/load
在上传完成时触发,无论服务器是否关闭连接.仅供参考,以下是您可以收听的事件以及它们何时触发:
The event you want to listen to is readystatechange
on the XHR object (not on XHR.upload). readyState
is 4
when the upload has finished sending and the server closes the connection. loadend
/load
fire when the upload has finished regardless of whether the server closes the connection. Just for reference, here are the events you can listen to and when they fire:
var xhr = new XMLHttpRequest();
// ...
// do stuff with xhr
// ...
xhr.upload.addEventListener('loadstart', function(e) {
// When the request starts.
});
xhr.upload.addEventListener('progress', function(e) {
// While sending and loading data.
});
xhr.upload.addEventListener('load', function(e) {
// When the request has *successfully* completed.
// Even if the server hasn't responded that it finished.
});
xhr.upload.addEventListener('loadend', function(e) {
// When the request has completed (either in success or failure).
// Just like 'load', even if the server hasn't
// responded that it finished processing the request.
});
xhr.upload.addEventListener('error', function(e) {
// When the request has failed.
});
xhr.upload.addEventListener('abort', function(e) {
// When the request has been aborted.
// For instance, by invoking the abort() method.
});
xhr.upload.addEventListener('timeout', function(e) {
// When the author specified timeout has passed
// before the request could complete.
});
// notice that the event handler is on xhr and not xhr.upload
xhr.addEventListener('readystatechange', function(e) {
if( this.readyState === 4 ) {
// the transfer has completed and the server closed the connection.
}
});
这篇关于XMLHttpRequest Level 2 - 确定上传是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!