文本文件下载链接(Ruby on Rails)

发布于 2024-11-24 19:22:08 字数 281 浏览 2 评论 0原文

我想将下载链接添加到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

世界和平 2024-12-01 19:22:08

使用rails send_file 方法

Use rails send_file method

梅倚清风 2024-12-01 19:22:08

您是否忘记在服务器端设置 wright content-header:

header("Content-Disposition: attachment; filename=\"myfile.txt\"");

Didn't you forget set wright content-header at server side:

header("Content-Disposition: attachment; filename=\"myfile.txt\"");
长伴 2024-12-01 19:22:08
  1. 简单的方法 - 压缩它。
  2. 如果可用 php
header ( 'Content-Type: text/html');
header ( "Content-Disposition: 'attachment'; filename='text.txt'" );
include ('path_to_file/myfile.txt')
exit;
  1. simple way - zip it.
  2. if is available php
header ( 'Content-Type: text/html');
header ( "Content-Disposition: 'attachment'; filename='text.txt'" );
include ('path_to_file/myfile.txt')
exit;
眼眸里的快感 2024-12-01 19:22:08

您可以在 .htaccess 中执行此操作

1. 如果您只想针对此特定文件:

<Directory path_to_file>
    <Files myfile.txt>
        <IfModule mod_headers.c>
            ForceType application/octet-stream
            Header set Content-Disposition attachment
        </IfModule>
    </Files>
</Directory>

2. 如果您希望针对 path_to_file 下的所有 .txt 文件

<Directory path_to_file>
    <FilesMatch “.(?i:(txt))$”>
        <IfModule mod_headers.c>
            ForceType application/octet-stream
            Header set Content-Disposition attachment
        </IfModule>
    </FilesMatch>
</Directory>

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>
溺ぐ爱和你が 2024-12-01 19:22:08

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

墨洒年华 2024-12-01 19:22:08

如果您的服务器支持 php,您可以使用以下行:

header('Content-type: text/plain');
header('Content-disposition: attachment; filename="name.txt"');
readfile('name.txt');

另请参阅 PHP: header例子#1

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文