我正在使用 XMLHttpRequest 执行 ajax 请求,以显示请求的进度。它对于 html 文件工作得很好,但是 evt.lengthComputable 对于 php 文件返回 false。
我的 php 文件以 utf-8 编码,不包含任何特殊内容。
xhr: function()
{
console.log('xhr');
var xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', uploadComplete, false);
function uploadComplete(event) {
console.log('uploadComplete');
//do stuff
}
//Download progress
xhr.addEventListener("progress", function(evt){
console.log([evt.lengthComputable, evt.loaded, evt.total]);
if (evt.lengthComputable) {
var percentComplete = (evt.loaded / evt.total) * 100;
}
}, false);
return xhr;
}
感谢您的帮助:)!
由于 php 文件是动态的,因此您需要设置正确的 Content-Length 标头:
<?
ob_start();
/* your code here */
$length = ob_get_length();
header('Content-Length: '.$length."\r\n");
header('Accept-Ranges: bytes'."\r\n");
ob_end_flush();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)