Post by Peter Cooper on June 9th, 2006

Streaming programmatically generated content from Rails

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

[Post to Twitter] Tweet This

Comments are closed.