Jump to content

Browser isn't rendering ob_flush output immediately


omdeshpande
Go to solution Solved by omdeshpande,

Recommended Posts

I'm writing a file upload script using the iframe method. At the backend, post file upload, the script does a few more things. So I'm flushing output to the browser (which goes to the iframe) as soon as the file is uploaded and then the script continues. From the Chrome console I can see that the response has been received by the browser (200 OK), but, for some reason the browser only renders it after the entire script has finished, causing quite a long delay. Why is this happening? How can I get the browser to render it immediately?

I have read a number of articles, blogs and Q/A on this topic that gave me the following hints, but none of them seem to be working:

- Some browsers require a minimum number of bytes to start rendering (256/1024/4096).

- If closing tags are missing, browsers tend to wait for it before they start rendering.

- Content length is required, otherwise the browser may be keep waiting for more data.

I have the above covered, I'm I missing something else?

Backend code:
$r = "
  <!DOCTYPE html>
  <head>
  <title></title>
  </head>
  <body>
  <p id='response'>$response</p>
  <p>"
.str_repeat(".", 4096)."</p>
  </body>
  </html>
"
;
ignore_user_abort(true);
set_time_limit(0);
ob_end_clean();
ob_start();
echo $r;
header("Content-Length: ".ob_get_length());
header("Connection: close", true);
header("Content-Encoding: none");
ob_end_flush();
flush();

Frontend code:
var iframe = document.getElementById("uploaddocframe_" + aid);
var iframeobj = (iframe.contentWindow || iframe.contentDocument);
if(iframeobj.document) {
  var iframedocument = iframeobj.document;
  var response = iframedocument.getElementById("response").innerHTML;
  if(response == "success"){
    //Do something
  }
  else {
    // Do something
  }
}

Link to comment
Share on other sites

  • Solution

Yeah, I gave up on this method.

 

AJAX isn't an option, because of data security. 

 

I solved this with a completely different approach. I implemented a "parallel processing via divide and rule" algorithm. More complex to implement, but much more stable and reliable. Post resume upload, I forked the code and deviated the tasks, post document upload, to another URL and then flushed that request immediately. Thereby, taking the browser out of the equation.

Link to comment
Share on other sites

Yeah, I gave up on this method.

 

AJAX isn't an option, because of data security. 

 

I solved this with a completely different approach. I implemented a "parallel processing via divide and rule" algorithm. More complex to implement, but much more stable and reliable. Post resume upload, I forked the code and deviated the tasks, post document upload, to another URL and then flushed that request immediately. Thereby, taking the browser out of the equation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.