我正在使用window.location.href在一个php函数中,该函数可以强制下载文件而不会打开其他窗口。文件下载成功并且php脚本的其余部分也执行了。然而,在函数中包含javascript代码的那一行之后输出的任何内容都不会显示在iframe中。有没有办法将输出重定向回原来的php脚本?
以下是php文件中的javascript代码:
echo "<script>window.location.href='download.php';</script>";
这里是download.php的代码:
<?php
session_start(); // Starts new or resumes existing session
$filename = $_SESSION['filename']; // Creates variable from session variable
header("Content-Type: text/plain"); // Output text file
header("Content-Disposition: attachment; filename=$filename"); // Output file name
readfile($filename); // Outputs the text file
?>
这是一个php文件中的示例,在javascript行后不会输出到iframe中。
echo 'Test Output';
提前致谢,
Jay
编辑
我用以下代码替换了javascript代码行,现在它完美地工作了:
echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";