send_file只发送一个空文件

时间:2010-08-11 07:21:10

标签: ruby-on-rails sendfile

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

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

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

为什么会这样?

Maechi

5 个答案:

答案 0 :(得分:24)

可能你必须发表评论

config.action_dispatch.x_sendfile_header = "X-Sendfile"

in production.rb

请参阅http://vijaydev.wordpress.com/2010/12/15/rails-3-and-apache-x-sendfile/了解说明

答案 1 :(得分:4)

问题已保存,但我不知道原因

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

send_data正在运行......但send_file没有!

答案 2 :(得分:3)

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

中建议的那一行
  

config / environments / production.rb文件。

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

答案 3 :(得分:3)

您必须在./config/environments/production.rb中启用sendfile用法:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

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

如果您要获得0字节文件,请确保已安装mod_xsendfilehttps://tn123.org/mod_xsendfile

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

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

答案 4 :(得分:0)

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

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'

拯救我的一天