Rails 中的 send_file
方法有返回值吗?如果发送成功,我想做一件事,如果发送不成功,我想做另一件事。我查看了文档this link ,但没有找到任何相关内容。
最佳答案
不,无法通过 send_file
确认下载完成或成功。来自这个问题:Can we find out when a Paperclip download is complete? :
send_file
creates the file and then passes a special header to tell the webserver telling it what to send. Rails doesn't actually send the file at all, it sets this header which tells the webserver to send the file but then returns immediately, and moves on to serve another request. To be able to track if the download completes you'd have to occupy your Rails application process sending the file and block until the user downloads it, instead of leaving that to the webserver (which is what its designed to do). This is super inefficient.
您也许可以在客户端上使用 cookie 和 JavaScript 执行某些操作。 看这个问题:Rails File Download And View Update - Howto?
关于ruby-on-rails - send_file返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188117/