Ruby on Rails 3:通过 Rails 将数据流式传输到客户端 [英] Ruby on Rails 3: Streaming data through Rails to client
问题描述
我正在开发一个与 RackSpace 云文件通信的 Ruby on Rails 应用程序(类似于 Amazon S3,但缺少一些功能).
I am working on a Ruby on Rails app that communicates with RackSpace cloudfiles (similar to Amazon S3 but lacking some features).
由于缺乏针对每个对象的访问权限和查询字符串身份验证的可用性,必须通过应用程序调解向用户的下载.
Due to the lack of the availability of per-object access permissions and query string authentication, downloads to users have to be mediated through an application.
在 Rails 2.3 中,您似乎可以按如下方式动态构建响应:
In Rails 2.3, it looks like you can dynamically build a response as follows:
# Streams about 180 MB of generated data to the browser.
render :text => proc { |response, output|
10_000_000.times do |i|
output.write("This is line #{i}\n")
end
}
(来自 http://api.rubyonrails.org/classes/ActionController/Base.html#M000464)
代替 10_000_000.times...
我可以在那里转储我的 cloudfiles 流生成代码.
Instead of 10_000_000.times...
I could dump my cloudfiles stream generation code in there.
问题是,这是我尝试在 Rails 3 中使用这种技术时得到的输出.
Trouble is, this is the output I get when I attempt to use this technique in Rails 3.
#<Proc:0x000000010989a6e8@/Users/jderiksen/lt/lt-uber/site/app/controllers/prospect_uploads_controller.rb:75>
看起来可能没有调用 proc 对象的 call
方法?还有其他想法吗?
Looks like maybe the proc object's call
method is not being called? Any other ideas?
推荐答案
Rails 3 中似乎没有此功能
It looks like this isn't available in Rails 3
https://rails.lighthouseapp.com/projects/8994/tickets/2546-render-text-proc
这在我的控制器中似乎对我有用:
This appeared to work for me in my controller:
self.response_body = proc{ |response, output|
output.write "Hello world"
}
这篇关于Ruby on Rails 3:通过 Rails 将数据流式传输到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!