Markus
Markus

Reputation: 4038

send_file just sends an empty file

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

Upvotes: 15

Views: 6555

Answers (5)

S.Yadav
S.Yadav

Reputation: 4509

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

Upvotes: 1

Jim
Jim

Reputation: 43

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.

Upvotes: 3

Benny Thomas
Benny Thomas

Reputation: 150

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"

Upvotes: 6

Eugene
Eugene

Reputation: 448

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

Upvotes: 24

Markus
Markus

Reputation: 4038

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!

Upvotes: 4

Related Questions