# Returning CSV data to the client

**URL:** <https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737>\
**Category:** Rails\
**Created:** [March 23, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737 "2006-03-23T12:48:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 23, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737/1 "2006-03-23T12:48:24Z")

</div>

Hi,

One of my apps has to export data for the backend system to process it.  
What’s the best way to create a CSV file in Rails and then return it as  
a file to the client? i.e when they go to the link /csv/get\_data it  
would return a csv file.

I’ve been messing with the CSV library and I can get it to write out to  
a file, but not sure how to best use this in a web app.

Hope someone can help… 🙂

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 23, 2006, 1:18pm UTC](https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737/2 "2006-03-23T13:18:04Z")

</div>

On Mar 23, 2006, at 12:48, Pete wrote:

> One of my apps has to export data for the backend system to process  
> it.  
> What’s the best way to create a CSV file in Rails and then return  
> it as  
> a file to the client? i.e when they go to the link /csv/get\_data it  
> would return a csv file.
> 
> I’ve been messing with the CSV library and I can get it to write  
> out to  
> a file, but not sure how to best use this in a web app.

For me it’s easier not to generate a file on disk, and work only in  
memory. This way I don’t have to worry about where to save the  
temporary file, and then when to remove it.

If that makes sense in your application, to generate a CSV in-memory  
you can do this:

def get\_data  
report = StringIO.new  
CSV::Writer.generate(report, ‘,’) do |csv|  
# csv \<\< row for each row  
end  
report.rewind  
send\_data(report.read, :type =\> ‘application/csv;  
charset=iso-8859-1’, :filename =\> ‘my\_data.csv’)  
end

– fxn

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 23, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737/3 "2006-03-23T16:41:52Z")

</div>

Perfect!

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 23, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/returning-csv-data-to-the-client/55737/4 "2006-03-23T16:50:03Z")

</div>

Hi !

2006/3/23, Pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’ve been messing with the CSV library and I can get it to write out to  
> a file, but not sure how to best use this in a web app.

There are two ways to do it. You can do as Xavier said and use the  
send\_data trick, or just make your RHTML file look like CSV.

I posted about both methods on my blog:  
[http://blog.teksol.info/articles/2006/03/23/returning-csv-data-to-the-browser](http://blog.teksol.info/articles/2006/03/23/returning-csv-data-to-the-browser)

Hope that helps !
