简体   繁体   English

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

[英]download the text file instead of opening in the browser

i have one text file: 我有一个文本文件:

When i click the Download it should download and save it in my local download path. 当我点击下载它应该下载并保存在我的本地下载路径中。

I have tried. 我努力了。

window.open("data.txt");

and

header("Location:data.txt")

But both are open the text file browser it self. 但两者都是自己打开文本文件浏览器。 I what download the txt file. 我下载了什么txt文件。

Any one please help me.. 任何人请帮助我..

Thanks 谢谢

Manikandan. Manikandan。

Try this: 尝试这个:

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

http标头可以解决您的问题,只需参考

 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Can you use this Jquery plugin http://jdownloadplugin.com/ to do the file download. 你可以使用这个Jquery插件http://jdownloadplugin.com/来进行文件下载。 It has many additional cool features for file downloading from web. 它还有许多额外的酷功能,可以从网上下载文件。

if you are using apache and want to enforce downloading the txt files instead of opening them in browser. 如果您正在使用Apache并希望强制下载txt文件而不是在浏览器中打开它们。 you can do using .htaccess 你可以使用.htaccess

AddType application/octet-stream .txt

我可以告诉你,你需要将标题内容 - 处置设置为文件附件,但我不知道如何从JavaScript中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM