Reputation: 16629
I want to add a download link to my html page. Download will be a .txt file. I have done this,
<a href="path_to_file/myfile.txt">click to download txt </a>
But the problem is, when a user clicks this link, instead of asking user to download the file, it simply shows the text in the browser.
How can I change this script to ask user to download the file (with the default download prompt dialog box)
UPDATE: Thanks all for the replies. I'm using ruby/rails on the server side.
Upvotes: 3
Views: 6657
Reputation: 1
View : (Html file)
= link_to 'click to download txt', :controller => 'download', :action => 'test'
Download Controller :
def test file_path = 'path_to_file/myfile.txt' send_file file_path end
Upvotes: 0
Reputation: 45589
You can do this in .htaccess
1. If you want only for this specific file:
<Directory path_to_file>
<Files myfile.txt>
<IfModule mod_headers.c>
ForceType application/octet-stream
Header set Content-Disposition attachment
</IfModule>
</Files>
</Directory>
2. If you want it to be for all the .txt
files under path_to_file
<Directory path_to_file>
<FilesMatch “.(?i:(txt))$”>
<IfModule mod_headers.c>
ForceType application/octet-stream
Header set Content-Disposition attachment
</IfModule>
</FilesMatch>
</Directory>
Upvotes: 0
Reputation: 2193
header ( 'Content-Type: text/html'); header ( "Content-Disposition: 'attachment'; filename='text.txt'" ); include ('path_to_file/myfile.txt') exit;
Upvotes: 0
Reputation: 1527
If your server supports php, you could use these lines:
header('Content-type: text/plain');
header('Content-disposition: attachment; filename="name.txt"');
readfile('name.txt');
Also see PHP: header Example #1
Upvotes: -2
Reputation: 22386
Didn't you forget set wright content-header at server side:
header("Content-Disposition: attachment; filename=\"myfile.txt\"");
Upvotes: 0