【发布时间】:2011-03-28 05:40:54
【问题描述】:
我正在寻找一种下载 xml 文件的方法。我用:
file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"
但这总是给我下载一个空文件。该文件本身包含 16 KB 的数据...
这是为什么呢?
前知
【问题讨论】:
我正在寻找一种下载 xml 文件的方法。我用:
file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"
但这总是给我下载一个空文件。该文件本身包含 16 KB 的数据...
这是为什么呢?
前知
【问题讨论】:
可能你要注释掉
config.action_dispatch.x_sendfile_header = "X-Sendfile"
生产中.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 中的帮助。
【讨论】:
在我的情况下,同样的事情发生了。
我正在发送文件并将其删除。
像这里
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'
拯救我的一天
【讨论】: