send_file只发送一个空文件

Mar*_*kus 13 ruby-on-rails sendfile

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

file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"
Run Code Online (Sandbox Code Playgroud)

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

这是为什么?

Maechi

小智 24

可能你必须发表评论

config.action_dispatch.x_sendfile_header = "X-Sendfile"

在production.rb

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


小智 6

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

config/environments/production.rb 文件。

# config.action_dispatch.x_sendfile_header = "X-Sendfile"
Run Code Online (Sandbox Code Playgroud)


Mar*_*kus 4

问题已解决,但不知道为什么

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

send_data 正在工作...但 send_file 没有!