Rails文件下载和查看更新 - Howto? [英] Rails File Download And View Update - Howto?
问题描述
这似乎应该是直截了当的,但我很难过。我有一个视图控制器的链接,最终使用send_data将文件下载到用户的硬盘驱动器。这非常有效,它使当前视图显然不受影响。
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.
但现在我希望该页面在下载完成后提供一些反馈。在send_data方法调用之前,我天真地在控制器中添加了类似下面的代码:
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"
但这不起作用,因为当前视图没有重新加载,让我有机会显示flash var。
But that doesn't work, because the current view doesn't reload to give me a chance to display the flash var.
我'我还尝试为此操作添加一个RJS视图,但这导致旧的DoubleRender错误,因为send_data也是一个渲染操作。
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.
所以......呃。 ..运行send_data后,如何将数据传回当前视图?或者是否有另一种解决此问题的方法?
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?
谢谢!
Aaron。
Thanks! Aaron.
推荐答案
也许你可以在响应中设置一个cookie,并在链接时使用javascript对该cookie进行轮询单击下载文件。
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.
可以像这样设置cookie:
The cookie can be set like this:
cookies["download_finished"] = "true"
send_data file, :filename=>fullname+".txt", :type=>"text/plain"
然后定期使用您最喜欢的javascript框架获取该cookie。
Then just periodically for that cookie using your favorite javascript framework.
这篇关于Rails文件下载和查看更新 - Howto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!