7

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.

Manikandan Thangaraj
  • 1,554
  • 7
  • 24
  • 43

5 Answers5

6

Try this:

$file = "data.txt";
$text = file_get_contents($file);
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
Sergey Ratnikov
  • 1,276
  • 7
  • 12
4

http headers may solve your problem, just referr

 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Rajasekar Gunasekaran
  • 1,740
  • 3
  • 23
  • 38
0

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.

DSharper
  • 3,137
  • 9
  • 28
  • 46
0

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
Vamsi Krishna B
  • 11,039
  • 14
  • 64
  • 93
-1

I can tell you that you need to set header content-disposition as file attachment, but I do not know how to do it from JavaScript.

Dmitriy R
  • 603
  • 1
  • 5
  • 11