Javascript 下载文件时禁用打开弹出窗口

Javascript 下载文件时禁用打开弹出窗口,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在创建一个wordpress页面,允许用户在5秒后下载zip文件。在其中,我调用第二个页面并传递POST参数(zip id::获取zip路径)。页面被调用,但始终作为弹出窗口。我正在寻找一个干净的下载选项,无需打开选项卡或新窗口,即可开始下载。绕过弹出窗口拦截器也 我试过两种方法 A) 方法1(Jquery Post) $.post( "", {附件_路径:“ } B) 方法2(提交表格) $(' 您可以通过重定向到php脚本的路径来实现这一点,该脚本将如何输出具有正确HTTP头的所需文件

我正在创建一个wordpress页面,允许用户在5秒后下载zip文件。在其中,我调用第二个页面并传递POST参数(zip id::获取zip路径)。页面被调用,但始终作为弹出窗口。我正在寻找一个干净的下载选项,无需打开选项卡或新窗口,即可开始下载。绕过弹出窗口拦截器也

我试过两种方法

A) 方法1(Jquery Post)

$.post(
"",
{附件_路径:“
}
B) 方法2(提交表格)

$('

您可以通过重定向到php脚本的路径来实现这一点,该脚本将如何输出具有正确HTTP头的所需文件

Javascript部分:

5秒钟后重定向到zip id为的“zipfetcher”脚本

<script>
function download(id) {
    setTimeout(function () {
        document.location = "zipfetcher.php?fileid="+id; // The path of the file to download
    }, 5000); // Wait Time in milli seconds
}
</script>

<a href="#" onclick="download('123');return false;">Click to download in 5 sec!</a> // Call of the download function when clicked

只需将标题设置为正确的类型,并调用URL。这是客户端的要求,首先打开下载页面,然后在5秒钟后开始文件下载。必须在不打开新窗口的情况下进行下载…不能允许直接链接到前端,这是脚本任务,需要获取和下载。但是,我想我们这里也有交互式窗口,要求是取消还是取消?我是rite吗@isaac@RahulGupta我刚刚更新了我的答案,向您展示了如何在隐藏完整路径的同时使用PHP脚本和Javascript来实现这一点,我只是在我的计算机上测试了它,它工作得很好;)@MayurRahul Evertyhing保持在同一页面上没有交互窗口here@Isaac我感谢你的努力,但在chrome中打开的弹出窗口(不需要)将使用您的代码片段。
$( '<form action="" method="post" target="_parent" class="hide">
       <input type="hidden" name="attachment_path" value="<?php echo $attachment[0]->ID ; ?>" />
       <input type="submit" name="submit" value="submit" id="target-counter-button"/>
    </form>'
   ).submit();  
<script>
function download(id) {
    setTimeout(function () {
        document.location = "zipfetcher.php?fileid="+id; // The path of the file to download
    }, 5000); // Wait Time in milli seconds
}
</script>

<a href="#" onclick="download('123');return false;">Click to download in 5 sec!</a> // Call of the download function when clicked
// Fetch the file path according to $_GET['fileid'] who's been sent by the download javascript function etc.
// ....

header('Content-Description: File Transfer');
header('Content-Type: application/zip, application/octet-stream'); // Puting the right content type in this case application/zip, application/octet-stream
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;