Re: [問題] 按鈕 下載本頁為htm檔

作者: ChenCH1986 (Chen,C.H.)   2014-05-21 17:23:21
※ 引述《ChenCH1986 (Chen,C.H.)》之銘言:
: 小弟想做一個功能, 爬過文但沒有找到解答
: 就是我想在一個php網頁裡頭實現 右鍵 另存新檔功能
: 例如原本是 a.php 右鍵另存新檔會變成 <網頁title>.htm
: 網頁title 正是我要的檔名, 而htm也是我要的檔案格式
: 已經試過使用execCommand("SaveAs")這功能
: 但是下載下來會變成a.php
: 這結果並不是我所想要的, 因此想詢問版上的高手
: 有甚麼方法能實現這個功能?
自問自答
首先先設置一個Form
<form action="a.php" method="get">
<input type="button" value="download">
<input id="pass_value" type="hidden" value="要傳的參數">
</form>
input 的 hidden 是用來儲存要給php值的
因為這一頁php原本是有接一個參數去讀取東西, 因此要透過這個hidden傳
然後在<html>之前 寫下
if(isset($_GET['download'])) {
header('Content-type: html');
header('Content-Disposition: attachment; filename= '.$_GET['pass_value'].');
//這一行可以用來設置下載後的檔案名稱
}
這樣就搞定了

Links booklink

Contact Us: admin [ a t ] ucptt.com