use*_*958 3 php xmlhttprequest
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
Thanx帮助:)!
因为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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5049 次 |
| 最近记录: |