gpt4 book ai didi

php - 浏览器没有立即呈现 ob_flush 输出

转载 作者:行者123 更新时间:2023-11-30 06:30:26 25 4
gpt4 key购买 nike

我正在使用 iframe 方法编写文件上传脚本。在后端,文件上传后,脚本还做了一些事情。因此,一旦文件上传,我就会将输出刷新到浏览器(进入 iframe),然后脚本继续。从 Chrome 控制台我可以看到浏览器已经收到响应(200 OK),但是由于某种原因,浏览器只在整个脚本完成后才呈现它,造成相当长的延迟。为什么会这样?如何让浏览器立即呈现它?

我已经在 SO 和外部阅读了很多关于这个主题的文章、博客和 Q/A,它们给了我以下提示,但它们似乎都没有用: - 某些浏览器需要最少的字节数才能开始呈现 (256/1024/4096)。 - 如果缺少结束标记,浏览器往往会在开始呈现之前等待它。 - 内容长度是必需的,否则浏览器可能会一直等待更多数据。

我已经涵盖了上述内容,我还缺少其他内容吗?

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 代码中,添加一个内容类型 header :

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

关于php - 浏览器没有立即呈现 ob_flush 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com