文件下载被浏览器弹出窗口阻止程序阻止

File download is being blocked by the browser popup blocker

我在 php 中有一个调用下载文件的文件下载功能,但浏览器弹出窗口阻止程序阻止它打开。

目标是有一个 html 表单提交按钮,用于提交表单,根据表单输入在服务器上编辑 rtf 文件,然后将新文件下载给用户。

因为我需要 php 到 运行 一些代码,在用户点击下载之后和文件下载之前我无法通过任何其他方式让它工作。我不得不让 php 回显一个 link,然后是一些点击 link.

的 js 代码

下载功能:

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:

<?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"]);

你可以用 header 代替 echo 并使用 JavaScript :

header ('location: download.php?path='. $path .'&name='. $name);

这将重定向到同一页面中的 download.php 并建议下载文件。