[英]File download is being blocked by the browser popup blocker
I have a file download function in php that calls the download file, but the browser popup blocker is preventing it from opening. 我在php中有一个文件下载功能,该功能会调用下载文件,但是浏览器弹出窗口阻止程序阻止了它的打开。
The goal is to have an html form submit button which submits a form, edits an rtf file on the server based on the form inputs, then downloads the new file to the user. 目的是要有一个html表单提交按钮,该按钮提交一个表单,根据表单输入在服务器上编辑rtf文件,然后将新文件下载给用户。
Because I need php to run some code after the user clicks download and before the file is downloaded I couldn't get it to work any other way. 因为在用户单击下载之后并且在下载文件之前,我需要php来运行一些代码,所以我无法使其以任何其他方式工作。 I had to make php echo a link, then some js code that clicks that link.
我必须使php回显一个链接,然后单击该链接的一些js代码。
The download function: 下载功能:
function download($path, $name){
if(!file_exists($path)){
echo $path." does not exist.";
}
else{
echo "
<a href='download.php?path={$path}&name={$name}' id='download'
target='download_frame' style='display:none;'>Download File</a>
<script>document.getElementById('download').click();</script>
";
}
}
download.php: 的download.php:
<?php
header("Content-type: application/doc");
basename($_GET["path"]));
$name = $_GET["name"];
header("Content-Disposition: attachment; filename='".$name.".doc");
header("Content-Transfer-Encoding: quoted_printable");
header('Pragma: no-cache');
header('Expires: 0');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Transfer-Encoding: binary');
header("Content-type: application/force-download");
header('Content-Type: application/octet-stream');
readfile($_GET["path"]);
You can do a header in place of echo and using JavaScript : 您可以使用JavaScript来代替header的标头:
header ('location: download.php?path='. $path .'&name='. $name);
This will redirect to the download.php in same page and propose to download the file. 这将重定向到同一页面中的download.php并建议下载文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.