ruby-on-railsruby-on-rails-4.2sendfilex-sendfile

Rails server file download crashing at send_file


I am working on downloading a file. My rails server crashes every time I download - when it reaches the send_file method.

Controller code:

file_path = '/home/rails/rails_app/public/test.csv'
send_file(file_path, options: {disposition: 'attachment', filename: File.basename(file_path)})

Server logs:

Sent file /home/rails/rails_app/public/test.csv  (0.1ms)
server #

This code was working on rails 2.X perfactly , but does not work while upgrading to rails 4.2.7.1


Solution

  • Can you try this and check?

    File.open(file_path, 'r') do |f|
      send_data f.read
    end