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,594
  • 8
  • 24
  • 44

5 Answers5

7

Try this:

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

http headers may solve your problem, just referr

 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Rajasekar Gunasekaran
  • 1,799
  • 3
  • 24
  • 40
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,177
  • 9
  • 29
  • 47
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,377
  • 15
  • 68
  • 94
-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
  • 613
  • 1
  • 5
  • 12