繁体   English   中英

用户下载文件后如何显示Thankyou页面?

[英]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>

thankyou.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其中download是一个ID,您可以获取正确的文件。 然后,您可以通过在标记中回显它,将该变量传递给下载脚本,如下所示:

的index.php

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

thankyou.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 to download file')打开”下载页面“”

例:

<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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM