[英]possible to detect loading complete in iframe with content-disposition: attachment?
I have this:我有这个:
html: html:
<form action="myfile.php" enctype="multipart/form-data" target="ifr1" method="post">
...form stuff...
</form
<iframe name="ifr1" id="ifr1" onload="console.log('loaded')"></iframe>
The php file contains these headers: php 文件包含以下标头:
header('Content-type: application/pdf');
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($out_filename));
header('Content-Disposition: attachment; filename="' . $pdf_filename . '"');
@readfile($out_filename);
When the php loads and I get the dialog box to download the pdf, the onload event on the iframe does NOT fire.当 php 加载并且我得到对话框以下载 pdf 时,iframe 上的 onload 事件不会触发。 Is there any way to have the front end detect when the php file has completed and the pdf file is ready?当 php 文件完成并且 pdf 文件准备好时,有什么方法可以让前端检测到?
You could try setting the content disposition to inline.您可以尝试将内容处置设置为内联。
Then use the ifr1's onload to begin the download.然后使用 ifr1 的 onload 开始下载。
I tested this script like this.我像这样测试了这个脚本。
<!DOCTYPE html>
<html lang="en">
<head><title>Download PDF</title>
<style></style></head><body>
<iframe id='ifr1' src="./myfile.php" height="200" width="300" onload="dlpdf()"></iframe>
<script>
function dlpdf(){
document.getElementById('ifr1').contentWindow.download();
}
</script>
</body></html>
And this is myfile.php这是 myfile.php
<?php
ob_start();
ob_clean();
$file = "./myfile.pdf";
header('HTTP/1.1 200 OK');
header('Connection: Keep-Alive');
header('Cache-Control: private, max-age=0');
header('Content-Length: ' . filesize($file));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Accept-Ranges: bytes');
header('Keep-Alive: timeout=5, max=100');
readfile($file);
ob_end_flush();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.