文本文件下载链接(Ruby on Rails)
我想将下载链接添加到我的 html 页面。下载将是一个 .txt 文件。我已经这样做了,
<a href="path_to_file/myfile.txt">click to download txt </a>
但问题是,当用户单击此链接时,它不会要求用户下载文件,而是仅在浏览器中显示文本。
我如何更改此脚本以要求用户下载文件(使用默认的下载提示对话框)
更新:感谢大家的回复。我在服务器端使用 ruby/rails。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用rails send_file 方法
Use rails send_file method
您是否忘记在服务器端设置 wright content-header:
Didn't you forget set wright content-header at server side:
您可以在 .htaccess 中执行此操作
1. 如果您只想针对此特定文件:
2. 如果您希望针对
path_to_file
下的所有.txt
文件You can do this in .htaccess
1. If you want only for this specific file:
2. If you want it to be for all the
.txt
files underpath_to_file
View : (Html file)
= link_to '点击下载txt', :controller => '下载', :action => “测试”
下载控制器:
def test
file_path = 'path_to_file/myfile.txt'
发送文件文件路径
结尾
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
如果您的服务器支持 php,您可以使用以下行:
另请参阅 PHP: header例子#1
If your server supports php, you could use these lines:
Also see PHP: header Example #1