Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails File Download And View Update - Howto?

This seems like it should be straight-forward, but I'm stumped. I have a link to a view controller that ends up using send_data to download a file to the user's hard drive. This works very well, and it leaves the current view apparently untouched.

But now I would like the page to provide some feedback after that download is complete. I naively put something like the following code in the controller before the send_data method call:

flash[:notice] = "Nice work, hot shot!"
send_data file, :filename=>fullname+".txt", :type=>"text/plain"

But that doesn't work, because the current view doesn't reload to give me a chance to display the flash var.

I've also tried adding an RJS view for this action, but that resulted in the old DoubleRender error, because send_data is a render action as well.

So... uh... how the heck would one pass data back to the current view after running send_data? Or is there another approach to this problem?

like image 202
Aaron Vegh Avatar asked Oct 13 '22 22:10

Aaron Vegh


1 Answers

Perhaps you could set a cookie in the response and poll for that cookie with javascript when the link to download the file is clicked.

The cookie can be set like this:

cookies["download_finished"] = "true"
send_data file, :filename=>fullname+".txt", :type=>"text/plain"

Then just periodically for that cookie using your favorite javascript framework.

like image 74
Jason stewart Avatar answered Oct 16 '22 15:10

Jason stewart