send_file just sends an empty file

情到浓时终转凉″ 提交于 2019-12-03 23:58:44

问题


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


回答1:


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




回答2:


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!




回答3:


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"



回答4:


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.



来源:https://stackoverflow.com/questions/3456166/send-file-just-sends-an-empty-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!