简体   繁体   English

可以检测到 iframe 中的加载完成,内容处置:附件?

[英]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.

相关问题 beforeunload 事件和 Content-Disposition=attachment - beforeunload event and Content-Disposition=attachment XMLHttpRequest 是否阻止了 Content-Disposition 附件? - Is Content-Disposition attachment blocked from XMLHttpRequest? 解压一个内容处理的 gzip 文件:附件 - Unpack a gzip file that is content-disposition: attachment 内容处置:附件未触发下载对话框 - Content-Disposition:attachment not triggering download dialog 通过PHP的标题下载文件后,将document.readyState的状态更改为“完成”(“ Content-Disposition:附件…” - changing state of document.readyState to “complete” after downloading file via PHP's header('Content-Disposition: attachment…' 如何通过javascript设置content-disposition = attachment? - how to set content-disposition = attachment via javascript? 内容处置始终为 null - Content-Disposition is always null 文件下载过程不会启动客户端(即使使用Content-Disposition:附件; filename = <filename> ) - File download process does not start client side (even with Content-Disposition: attachment ; filename=<filename>) 如何与content-disposition:附件暂时停用onbeforeun-message? - How to temporally deactivate onbeforeunload-message in combination with content-disposition: attachment? 将内容处置与 Axios 和 NodeJS 一起使用 - Use content-disposition with Axios and NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM