簡體   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