A download request is sent and the echo returns a response in the form of html. If its size is 1.5 Kb, the progress bar works. At a size of 75 KB, lengthComputable returns false .

Why isn't the size of the downloadable response being determined or what am I doing wrong?

Js

$.ajax({ url: 'page.php', xhrFields: { onprogress: function(e) { /* set progressbar value */ console.log(e.loaded); console.log(e.lengthComputable); } }, type: 'POST', data: {'page': id}, success: function(data) { content.append(data); } }); 

Php

 if(isset($_POST['page'])) { // Извлечение данных из базы и формирование блока с элементами echo '<div class="item">...</div>'; } 

    1 answer 1

    Found the answer

     ob_start(); echo 'data'; $length = ob_get_length(); header('Content-Length: '.$length."\r\n"); header('Accept-Ranges: bytes'."\r\n"); ob_end_flush(); 

    To get the size of the content from the server ( lengthComputable ), you just need to write the length of the content in the header. In my case, I used the length of characters in the variable of the loadable response from the server

     header('Content-Length: '.strlen($html)); 
    • clean water kastilje you don’t need to enter at all, so remove the evo leaving the header - Naumov
    • @naumov, where is the crutch? There is a beginning, through the cycle of formation of the item from the base, the length of the header is assigned to a variable, after the cycle the header is set and the data is sent, after which the buffer is cleaned - Mr. Black
    • So you do not use the buffer here and the caste - Naumov
    • @Naumov, thank you, if I didn’t say what a crutch, I wouldn’t check and couldn’t find the joint - Mr. Black