Reputation: 15247
I want to download a file for each value in my array in a loop.
I have two methods in my class:
def test
testarray = ['value1', 'value2', 'value3']
testarray.each do |value|
download_file(value)
end
end
def download_file(value)
send_file("public/downloads/test/" + value +".pdf", :filename => value + ".pdf", :type => "application/pdf", :diposition => "inline")
end
But when I call my test method, only the last element of my array is downloaded. The console prints the following:
Sent file public/downloads/shapefiles/test1.pdf (0.1ms)
Sent file public/downloads/shapefiles/test2.pdf (0.1ms)
Sent file public/downloads/shapefiles/test3.pdf (0.1ms)
Completed 200 OK in 2ms (ActiveRecord: 0.0ms)
But my browser downloads only the last file, in this case, the test3.pdf
.
Upvotes: 1
Views: 2317
Reputation: 18474
send_file
actually instructs the server to send a file, not sends it itself. For example if properly configured nginx is used - rails will only issue X-Accel-Redirect
header, all other file handling will be done by nginx. Other setups behave generally the same (but without a reverse proxy file upload still will block some ruby process/thread)
That's why only last file is being sent.
So to download multiple files they should be packed into one. You can use nginx_mod_zip to create zip on the fly using technique similar to X-accel-redirect
Upvotes: 2