Streaming programmatically generated content from Rails
By Peter Cooper / June 9, 2006
Rather than use the send_file or send_data methods to send an entire, completed bulk of information back to the client, David N. Welton wanted to, effectively, print stuff to the client bit by bit. He worked out how to do it and presents his findings and sample code here.
A preview of his 'output data to CSV line by line' method:
def generate_file @headers["Content-Type"] = "text/comma-separated-values;" @headers["Content-Disposition"] = "filename="some.file.txt";" i = 0 render :text => Proc.new { |response, output| ... output.write("some generated text...") ... }, :layout => false end
Other Posts to Enjoy
-
Ruby’s Unary Operators and How to Redefine Their Functionality
Ruby’s Unary Operators and How to Redefine Their Functionality
-
The Split is Not Enough: Unicode Whitespace Shenigans for Rubyists
The Split is Not Enough: Unicode Whitespace Shenigans for Rubyists
-
The Mega Ruby News and Release Roundup for February 2012
The Mega Ruby News and Release Roundup for February 2012
-
A Lagom Review of O’Reilly’s ‘Sinatra Up and Running’
A Lagom Review of O’Reilly’s ‘Sinatra Up and Running’