1

From ajax I call my export action in my controller

def export

    data_filtered = eval(params[:data_filtered][9..-2])
    CSV.open("data.csv", "wb") do |csv|
        csv << data_filtered.first.keys
        data_filtered.each do |hash|
            csv << hash.values
        end
     end
 end

Currently this does indeed create a file called data.csv on my computer but it doesn't show any evidence of actually doing so. I want the file to be downloaded by the browser and show up in the browser downloads and also in my downloads folder.

EDIT:

def export
  data_filtered = eval(params[:data_filtered][9..-2])

  csv_file = CSV.generate({}) do |csv|
  csv << data_filtered.first.keys
  data_filtered.each do |hash|
    csv << hash.values
  end
end
send_data csv_file, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=tester1.csv"

end

2 Answers2

0

You need to set headers to indicate what the browser should be doing with the resulting file.

send_data file, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment;data=#{csv_file}.csv"

However, it would have to be written first, to the server, and then downloaded. You might find this answer helpful: Ruby 1.9.2 export a CSV string without generating a file

Community
  • 1
  • 1
0

Well it appears you are not sending back any response from your controller method, which is why nothing is happening in the browser. You are just processing the CSV.

To send back a downloadable entity a response, one way would be to use either send_data or send_file, depending on what format you have the data in (binary vs. a file).

Both essentially do the same -- allow you to specify the data, the data type, and a downloadable file name that you specify. It can be returned as a response, just like render calls.

Hope that helps!

GoGoCarl
  • 2,519
  • 13
  • 16
  • Set the :filename option to something and it should be good to go. Also, now that you're doing it this way, you don't really need AJAX. Just a link will do. – GoGoCarl Jul 02 '13 at 15:11
  • But I am using ajax so that my page doesnt change –  Jul 02 '13 at 15:12
  • It won't change because you're downloading a file, it will just download the file and keep you on the page. For paranoia purposes you can open the page in a new window, but the result would be the same. – GoGoCarl Jul 02 '13 at 15:13
  • Double-check your code against this example: http://apidock.com/rails/ActionController/DataStreaming/send_data#1270-Example-to-auto-download- Your code is nearly identical, minus a few tweaks. – GoGoCarl Jul 02 '13 at 15:25
  • where does it send the data too? i still dont have it on my computer and in the consol it says that it send the data successfully –  Jul 02 '13 at 15:32
  • Also, @zeeklon if you're still using AJAX method, try getting it to work first by simply visiting the appropriate link in a browser. In thinking about it, I'm actually not sure if you CAN download as an attachment using AJAX. Anyway, testing it this way will at least let you know that the controller target works. – GoGoCarl Jul 02 '13 at 15:33
  • Yup, take the AJAX out as I suggested and try just a browser link. Should work like a charm. – GoGoCarl Jul 02 '13 at 15:33
  • when i debug I am hitting the action export from ajax and there aren't any errors –  Jul 02 '13 at 15:34
  • Right, but AJAX has already "downloaded" the data, so the user can't. – GoGoCarl Jul 02 '13 at 15:35
  • you are right you can't call from ajax. if i dont it works great! –  Jul 02 '13 at 15:37