Topic: Best way to offer downloads to user?

I'd like to be able to generate excel and word documents on the fly, using remote (asynchronous) links if possible.  So, if I'm going to generate a .xls file (dependent on the current view and certain session options) and offer it to the user as a direct download, whats the best way to do this?

I'm using Spreadsheet::Excel for xls.  One solution I thought of was to generate the file and store it server-side, then offer a direct download of that file on the server.  Is this necessary, or can I generate the .xls file and push it to the client in one fell swoop?

Also, if I'm going to need to store the file server side and then offer a link to it, how can I code this into my controller?  Would I end the method with "redirect_to <url of file>"?

Re: Best way to offer downloads to user?


Take a look at the send_data method.

send_data should be the last line that you call from the action when you're serving the file.  If you're using layouts, be sure and add:

layout 'whatever', :except => [:name_of_my_download_method]

to the top of your controller as well!  This will prevent your layout from being rendered for that action.

Ruby Rockstars - Ruby Jobs for all!

Re: Best way to offer downloads to user?

Thanks so much!  I must say, this is turning into a really valuable resource!

Re: Best way to offer downloads to user?

Hi Will you please share the code on how you used send_data function?

Re: Best way to offer downloads to user?

The documentation is here: … ml#M000092

I honestly can't remember the exact code I used.  All you really have to do is generate the file you want to send, then call send_data at the end of your controller action instead of calling render.

Hope that helps