简体   繁体   中英

Browser isn't rendering ob_flush output immediately

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 on SO and outside 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
        }
    }

在您的PHP代码中,添加一个content-type标头:

header( 'Content-type: text/html; charset=utf-8' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM