簡體   English   中英

文件下載被瀏覽器彈出窗口阻止程序阻止

[英]File download is being blocked by the browser popup blocker

我在php中有一個文件下載功能,該功能會調用下載文件,但是瀏覽器彈出窗口阻止程序阻止了它的打開。

目的是要有一個html表單提交按鈕,該按鈕提交一個表單,根據表單輸入在服務器上編輯rtf文件,然后將新文件下載給用戶。

因為在用戶單擊下載之后並且在下載文件之前,我需要php來運行一些代碼,所以我無法使其以任何其他方式工作。 我必須使php回顯一個鏈接,然后單擊該鏈接的一些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"]);

您可以使用JavaScript來代替header的標頭:

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

這將重定向到同一頁面中的download.php並建議下載文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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