下载文本文件而不是在浏览器中打开

Man*_*raj 7 javascript php text

我有一个文本文件:

当我点击下载它应该下载并保存在我的本地下载路径中.

我试过了.

window.open("data.txt");
Run Code Online (Sandbox Code Playgroud)

header("Location:data.txt")
Run Code Online (Sandbox Code Playgroud)

但两者都是自己打开文本文件浏览器.我下载了什么txt文件.

任何人请帮助我..

谢谢

Manikandan.

Ser*_*kov 6

试试这个:

$file = "data.txt";
$text = file_get_contents($file);
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
Run Code Online (Sandbox Code Playgroud)


Raj*_*ran 4

http headers 可能会解决你的问题,只需引用

 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Run Code Online (Sandbox Code Playgroud)