4

I am using the send_file method to download a file. It works fine on my local machine. But it's not working on the server - it returns a blank file.

code:

send_file Rails.root.join('public', 'uploads') + (uploaded_file.original_filename + ".filtered")

Please help.

1
  • 1
    Does the file exist. i.e. If you goto yourdomain/public/uploads/filename.filtered does it work? Commented May 27, 2011 at 13:07

2 Answers 2

4

Depending on what web server you are using, you may have to change the sendfile settings in your config/environments/production.rb.

For example, for nginx, you need to uncomment out this line:

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

send_file Rails.public_path.join('uploads', "#{uploaded_file.original_filename}.filtered")

Note also that if you have files within the public directory these can be served directly, so you could redirect to the generated URL for the item (though this may not be very REST-ful).


Edit:

Because of the non-standard '.filtered' file extension, you may need to do something like:

send_file Rails.public_path.join('uploads', "#{uploaded_file.original_filename}.filtered"), :type => 'application/xml', :disposition => 'attachment'

Change the :type value to match the content type of the file being served.

4 Comments

As @Yule mentioned above, have you checked that the file exists in that location? Also, does the server process have read access to the directory? (note that the server process may be running under a user like 'www-data')
@jits thank you very much for your support. That one not works. Any other options?
@Sayuj I've updated the answer with another possible solution.
@Sayuj I've further edited the answer to use Rails.public_path instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.