send_file只发送一个空文件
我正在寻找一种下载xml文件的方法。 我用:
file_path = 'folder/' + xml_name + '.xml' send_file file_path, :type => "text/xml"
但这总是下载一个空文件。 文件本身有16 KB的数据……
这是为什么?
Maechi
可能你必须发表评论
config.action_dispatch.x_sendfile_header = "X-Sendfile"
在production.rb
请参阅http://vijaydev.wordpress.com/2010/12/15/rails-3-and-apache-x-sendfile/以获取解释
问题得救,但我不知道为什么
File.open(file_path, 'r') do |f| send_data f.read, :type => "text/xml", :filename => "10.xml" end
send_data正在工作……但send_file没有!
正如Eugene在他的回答中所说,在生产环境中,Rails会让Apache或者nginx用x-sendfile为你发送实际文件,如果你不使用其中任何一个作为rails的基础设施,你必须注释掉建议的行在里面
config / environments / production.rb文件。
# config.action_dispatch.x_sendfile_header = "X-Sendfile"
您必须在./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配置文件中设置XSendFile
和XSendFilePath
选项。 有关详细信息,请参阅上述URL中的帮助。