简体   繁体   English

浏览器未立即呈现ob_flush输出

[英]Browser isn't rendering ob_flush output immediately

I'm writing a file upload script using the iframe method. 我正在使用iframe方法编写文件上传脚本。 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. 因此,文件上传后,我便立即将输出刷新到浏览器(进入iframe),然后脚本继续。 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. 从Chrome控制台中,我可以看到浏览器已收到响应(200 OK),但是由于某种原因,浏览器仅在整个脚本完成后才呈现响应,从而导致相当长的延迟。 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). 我已经阅读了许多关于SO以及其他相关主题的文章,博客和问答,这些提示给了我以下提示,但是这些提示似乎都不起作用:-有些浏览器需要最小数量的字节才能开始渲染(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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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