我正在使用iframe方法编写文件上传脚本。在后端,文件上传后,脚本会做更多的事情。因此,一旦文件上传,我就会将输出刷新到浏览器(转到iframe),然后脚本继续。从Chrome控制台我可以看到浏览器已收到响应(200 OK),但由于某种原因,浏览器仅在整个脚本完成后才会呈现它,导致相当长的延迟。为什么会这样?如何让浏览器立即呈现它?
我已经在SO和外面阅读了很多关于这个主题的文章,博客和问答,给了我以下提示,但它们似乎都没有起作用: - 某些浏览器需要最少的字节数才能开始渲染(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
}
}
答案 0 :(得分:0)
在PHP代码中,添加内容类型标题:
header( 'Content-type: text/html; charset=utf-8' );