php - ajax xhr lengthComputable用php文件返回false

标签 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;
            }

感谢您的帮助:)!

最佳答案

因为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(); 

关于php - ajax xhr lengthComputable用php文件返回false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16034312/

相关文章:

angular - 如何使用最大并行请求数发送 1000 个 XHTTP 请求

javascript - nodejs xhr POST

PHP 数组替换

php - 在 PHP 上传临时文件夹中的文件之前检查文件类型 (FLV),方法是只读取文件的起始 3 个字节

PHP 高级作业队列

javascript - onClick 从 session 中删除项目

javascript - 如何创建 XMLHttpRequest FormData 表单对象

scala - 播放 2.2.1 SimpleResult 4xx 响应主体是否可以通过 CORS XHR?

Node.js - 将多个文件作为附件从服务器推送到客户端

php - 表单提交在 mysql 中显示空白条目