Download Rails Files and View Updates - Howto?

It seems like it should be straightforward, but I'm at a standstill. I have a link to a view controller that ends up using send_data to upload a file to the user's hard drive. This works very well, and it leaves the current view seemingly unaffected.

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

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

But this does not work, because the current view does not restart to give me the ability to display flash var.

I also tried to add an RJS view for this action, but this led to the error of the old DoubleRender, because send_data is also a rendering action.

So ... mm ... how the hell can pass the data back to the current view after running send_data? Or is there a different approach to this problem?

Thank! Aaron.

+3
source share
1 answer

Perhaps you can set a cookie in the response and survey for this javascript cookie when you click the link to download the file.

A cookie can be set as follows:

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

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

+2
source

Source: https://habr.com/ru/post/1769797/


All Articles