Ajax xhr lengthComputable return false with php file

I am making an ajax request with XMLHttpRequest to show the progress of the request. It works fine with the html file, but evt.lengthComputable returns false with the php file.

My php file is encoded in utf-8 and does not contain anything special.

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

Thanx for help :)!

+3
source share
1 answer

Since the php file is dynamic, you need to set the correct Content-Length header:

 <? ob_start(); /* your code here */ $length = ob_get_length(); header('Content-Length: '.$length."\r\n"); header('Accept-Ranges: bytes'."\r\n"); ob_end_flush(); 
+6
source

Source: https://habr.com/ru/post/1273569/


All Articles