點擊 .txt 鏈接瀏覽器直接顯示內容,需改為彈出下載對話框。
**後端動態輸出文件並設置 Header**:
// PHP 範例
$file = '/path/to/file.txt';
if (file_exists($file)) {
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
}
Content-Disposition: attachment; filename="xxx.txt"
此 Header 告訴瀏覽器「作為附件下載」而非內聯顯示。
response.setHeader("Content-Disposition", "attachment; filename=...")Response.AppendHeader("Content-Disposition", "attachment; filename=...")add_header Content-Disposition attachment; 或 ForceType application/octet-stream。