如何在用户下载文件后显示感谢页面


How can I display a Thankyou page after user downloads file?

我正在使用这个小推进器php文件下载一个"exe"文件,供用户在填写表格时保存。

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

使用这种方法,没有指向在线$文件位置的链接供用户关注,但由于某种原因,我无法在代码执行后甚至执行前立即显示感谢页面。

我试过标题("位置:$thankyouurl");在上面的代码之后,但什么都没有显示,我试着在运行上面的代码之前回显感谢页面的html源代码,但这会导致exe文件下载到网页,实际上会导致浏览器崩溃。

有什么建议吗???

将用户直接指向显示感谢信息的静态页面。然后,使用JavaScript将window.location设置为下载脚本。

如果下载页面的标题设置正确,浏览器将在用户阅读感谢信息时开始下载该文件。

确保在用户禁用JavaScript的情况下显示直接下载链接。

示例:

index.php

<a href="thankyou.php">Click here to download.</a>

谢谢.php

<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p>
</body>

download.php

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

更新

为了将数据从index.php传递到下载脚本,可以使用GET变量。指向thankyou.php页面的链接将变为thankyou.php?download=12,其中下载是获取正确文件的ID。然后,您可以通过在标记中回显该变量来将其传递到下载脚本,如下所示:

index.php

<a href="thankyou.php?download=12">Click here to download.</a>

谢谢.php

<html>
<head>
    <script type="text/javascript">
        function startDownload() {
            window.location = "/download.php?file=<?=$_GET['download']?>";
        }
    </script>
</head>
<body onload="startDownload();">
    <h1>Thank you!</h1>
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p>
</body>

然后,您可以获取下载脚本中的值,并以任何您喜欢的方式进行处理。

对此不太确定,会不会…

header('Location: thanks_page.php');

不起作用,。如果放在readfile()之后?真的不确定,只有我的想法!

您可以使用"windows.open('下载文件的url')"打开"下载页面"

示例:

<script type="text/javascript">
    function openDownload()
    {
        window.open( "http://domain.com/download.php?file=file.pdf" );
        window.location = "http://domain.com/thanks.php";
    }
</script>

打开一个直接下载文件的窗口,当您选择保存或取消时,该窗口将自动关闭,然后"父"窗口重定向到感谢页面。

使用原始方法,将header('location: thank-you-page.php');添加到末尾,并添加一个出口,如下所示:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
header('location: thank-you-page.php');
exit;