15

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

corroded
  • 21,406
  • 19
  • 83
  • 132
Markus
  • 3,948
  • 8
  • 48
  • 64

5 Answers5

24

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

Eugene
  • 448
  • 4
  • 12
6

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"
Benny Thomas
  • 151
  • 4
  • 4
4

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!

Markus
  • 3,948
  • 8
  • 48
  • 64
  • didnt worked for me.. both have the same issue in prod.. i checked the config for prod and the is disabled x_sendfile_header – mariomol Apr 11 '17 at 15:59
3

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.

Jim
  • 43
  • 1
  • 6
1

In my case same thing happened. I was sending file and deleting it.
as here

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)

but

filename: 'my_file.docx'

save my day

S.Yadav
  • 4,273
  • 3
  • 37
  • 44