簡體   English   中英

可以檢測到 iframe 中的加載完成,內容處置:附件?

[英]possible to detect loading complete in iframe with content-disposition: attachment?

我有這個:

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>

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);

當 php 加載並且我得到對話框以下載 pdf 時,iframe 上的 onload 事件不會觸發。 當 php 文件完成並且 pdf 文件准備好時,有什么方法可以讓前端檢測到?

您可以嘗試將內容處置設置為內聯。
然后使用 ifr1 的 onload 開始下載。
我像這樣測試了這個腳本。

<!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>

這是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM