简体   繁体   中英

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.

Any one please help me..

Thanks

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. 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. you can do using .htaccess

AddType application/octet-stream .txt

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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