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
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
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!
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"
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