Re: [請益] 詢問是否下載
※ 引述《claire0810 (Claire)》之銘言:
: 下載檔案時 有時候某些東西會自動開啟
: 例如圖檔 跟txt檔
: 請問 我該如何強迫他詢問是否要存檔還是要直接開啟
: (不好意思 我不知道這是屬於JS 或者PHP如果不是PHP我會自D)
這問題其實不錯,當初也困擾我不少時間,不知道的還真不知道怎作的
其實是跟 header 有關~
<?
// header前 不能有其他輸出字串
// 每個空格都滿重要的,要留意
header("Content-Disposition: attachment; filename=a.txt");
header("Content-Type: application/octet-stream");
// 以下放置內文的輸出
// 範例1:
echo "test 123";
// 範例2:
echo file_get_contents('http://3wa.tw');
// 範例3:
echo file_get_contents('orz.jpg');
?>
應該還有第二種方法,如設定 .htaccess (apache),不過小弟就沒試了
剛才又仔細看了一下原po的問題~發現有點沒切入主題
下面再補充一些好了
你可以把上面這支小改,作成 download.php
<?
$GET_STRING='filename,file';
foreach(explode(',',$GET_STRING) as $index)
{
$GETS[$index]=htmlspecialchars(trim($_GET[$index]));
}
header("Content-Disposition: attachment; filename={$GETS['filename']}");
header("Content-Type: application/octet-stream");
// 以下放置內文的輸出
echo file_get_contents($GETS['file']);
?>
如此一來,download.php可以吃二個參數
download.php?
filename=a.txt&
file=a.jpg
然後~你的js就可以作個詢問的動作了
<script language="javascript">
var question=confirm('你要直接開啟嗎?','問的很好...');
if(question==true)
{
window.open('a.jpg');
}
else
{
window.open('download.php?filename=a.jpg&file=a.jpg');
}
</script>
--
3WA訓練家的工作室
宗旨:人生就是要爽
個人佈弱格 網址:http://3wa.tw
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.47.155
※ 編輯: shadowjohn 來自: 122.116.47.155 (11/30 15:57)
推
11/30 17:09, , 1F
11/30 17:09, 1F
→
11/30 17:12, , 2F
11/30 17:12, 2F
推
11/30 17:22, , 3F
11/30 17:22, 3F
→
11/30 17:22, , 4F
11/30 17:22, 4F
→
11/30 17:23, , 5F
11/30 17:23, 5F
※ 編輯: shadowjohn 來自: 123.240.117.97 (08/17 17:30)
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章
0
18