send_file 只发送一个空文件

发布于 2024-09-13 10:23:43 字数 207 浏览 1 评论 0原文

我正在寻找一种下载 xml 文件的方法。我使用:

file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"

但这总是下载一个空文件。文件本身有 16 KB 的数据...

为什么呢?

前智

Im looking for a way to download a xml file. I use:

file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"

but this always downloads me an empty file. The file itself has 16 KB of data in it...

why is that?

Maechi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

獨角戲 2024-09-20 10:23:43

注释掉

config.action_dispatch.x_sendfile_header = "X-Sendfile"

可能您必须在 Production.rb 中

请参阅 http://vijaydev.wordpress.com/2010/12/15/rails-3-and-apache-x-sendfile/ 寻求解释

probably you have to comment out

config.action_dispatch.x_sendfile_header = "X-Sendfile"

in production.rb

see http://vijaydev.wordpress.com/2010/12/15/rails-3-and-apache-x-sendfile/ for explanations

自由如风 2024-09-20 10:23:43

正如 Eugene 在他的回答中所说,在生产环境中,Rails 将让 Apache 或 nginx 使用 x-sendfile 为您发送实际文件,如果您不使用其中任何一个作为 Rails 的基础设施,则必须注释掉建议的行在

config/environments/production.rb 文件。

# config.action_dispatch.x_sendfile_header = "X-Sendfile"

As Eugene says in his answer, in a production enviroment Rails will let Apache or nginx send the actual file for you with x-sendfile, if you don't use either of these as the infrastructure for rails you have to comment out the line suggested in the

config/environments/production.rb file.

# config.action_dispatch.x_sendfile_header = "X-Sendfile"
弥繁 2024-09-20 10:23:43

问题已保存,但我不知道为什么

File.open(file_path, 'r') do |f|
  send_data f.read, :type => "text/xml", :filename => "10.xml"
end

send_data 有效...但 send_file 无效!

Problem saved, but I don't know why

File.open(file_path, 'r') do |f|
  send_data f.read, :type => "text/xml", :filename => "10.xml"
end

send_data is working... but send_file not!

表情可笑 2024-09-20 10:23:43

您必须在 ./config/environments/Production.rb 中启用 sendfile 使用:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

如果此行不存在(或被注释掉),则 Rails 将正确发送文件,但不会通过 Apache。

如果您获得 0 字节文件,请确保您已安装 mod_xsendfile,该文件可从 获取https://tn123.org/mod_xsendfile

下载单个源文件 (mod_xsendfile.c) 并编译它 (apxs -cia mod_xsendfile.c)。您可能希望以 root 身份运行 apxs,以便正确设置所有内容。

然后,您需要在 Apache 配置文件中设置 XSendFileXSendFilePath 选项。有关详细信息,请参阅上述 URL 中的帮助。

You must enable sendfile usage in ./config/environments/production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

If this line is not present (or commented out), then Rails will correctly send the file, but not through Apache.

If you are getting 0-byte files, then make sure that you have installed mod_xsendfile, which is available from https://tn123.org/mod_xsendfile

Download the single source file (mod_xsendfile.c) and compile it (apxs -cia mod_xsendfile.c). You probably want to run apxs as root so that it will set up everything correctly.

Then you're going to want to set the XSendFile and XSendFilePath options in your Apache configuration files. See the help at the above URL for more information.

风流物 2024-09-20 10:23:43

就我而言,同样的事情也发生了。
我正在发送文件并删除它。
和这里一样

File.open(file_path, 'r') do |f|      
  send_data f.read, filename: 'my_file.docx' , type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', disposition: 'attachment'
end
File.delete(file)

,但

filename: 'my_file.docx'

拯救我的一天

In my case same thing happened.
I was sending file and deleting it.
as here

File.open(file_path, 'r') do |f|      
  send_data f.read, filename: 'my_file.docx' , type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', disposition: 'attachment'
end
File.delete(file)

but

filename: 'my_file.docx'

save my day

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