我有一个包含许多文件上传的表单,该表单需要一段时间才能提交。有没有办法用js或jquery或php来获取表单提交的进度?
是的,这是可能的,因为 XHR 更新随 HTML5 一起提供(客户端,纯 JavaScript)。查看 XHR 在 html5rocks.com 上的更新,尤其是在上传文件或 blob 详细信息时:如下所示:
.HTML
<progress min="0" max="100" value="0">0% complete</progress>
.JS
function upload(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.onload = function(e) { ... };
// Listen to the upload progress.
var progressBar = document.querySelector('progress');
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
progressBar.value = (e.loaded / e.total) * 100;
progressBar.textContent = progressBar.value; // Fallback for unsupported browsers.
}
};
xhr.send(blobOrFile);
}
upload(new Blob(['hello world'], {type: 'text/plain'}));