Reputation: 4706
I'm wondering if it's possible to do this in Rails:
Have a link on the webpage. When the user clicks on the link, the controller generates a file dynamically (say, a text file that contains a single random number between 1 and 10), and the file is downloaded onto the user's computer. The file might be temporarily stored on the server, but it shouldn't be permanently there.
Upvotes: 5
Views: 2062
Reputation: 11904
Yes, it's possible. This is what I have in one of my apps:
class DownloadsController < ApplicationController
def download
# ...
send_file CSVConstructor::Constructor.new(...).to_zip
end
end
The download
action takes params submitted from a form and sends them to a custom class that generates a few files, packages them as a zip, and returns the file path. You'll have to figure out the best way to generate files for your own app, but I would recommend something similar - branching the functionality into a separate class helps keep your controller light.
Upvotes: 3
Reputation:
Use send_data
in the controller:
send_data("4", :filename => "my_awesome_file")
If you already have the file on the server, you can use send_file
instead
send_file(filepath, :filename => "my_awesome_file")
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
Upvotes: 7