# Best way for sending file to user

**URL:** <https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461>\
**Category:** Ruby\
**Created:** [February 7, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461 "2008-02-07T21:44:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 7, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461/1 "2008-02-07T21:44:48Z")

</div>

HI, I’m currently using the FasterCSV library to pull a bunch of data  
from my database and send a .csv file to the user. Currently i start off  
with

FasterCSV.open(“app/models/” + @assessment.assessment + “.csv”, “w”) do  
|csv|

write all the info to it, then i use

send\_file (‘app/models/’ + @assessment.assessment + ‘.csv’)

to send it. It saves the csv file on the server and then sends that file  
to the user. Is there any way to send the csv file without saving it to  
the server? Or is there a command i can use to delete the file after it  
sends? I couldnt quite find any answers to what I’m trying to do yet.  
Thanks alot if anyone has input 🙂

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 7, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461/2 "2008-02-07T22:17:17Z")

</div>

On Feb 7, 2008, at 2:44 PM, Mark Mr wrote:

> send\_file (‘app/models/’ + @assessment.assessment + ‘.csv’)
> 
> to send it. It saves the csv file on the server and then sends that  
> file  
> to the user. Is there any way to send the csv file without saving it  
> to  
> the server?

Yes, if you can easily hold the data in memory. You could use  
FasterCSV.generate() to get the data in a String. The use send\_data()  
instead of send\_file() to dump it to the client.

> Or is there a command i can use to delete the file after it  
> sends?

Sure, use the standard Tempfile library to allocate a file, write to  
it, and then feed it to send\_file().

James Edward G. II

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 7, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461/3 "2008-02-07T22:32:35Z")

</div>

On Fri, Feb 08, 2008 at 05:44:51AM +0900, Mark Mr wrote:

> to send it. It saves the csv file on the server and then sends that file  
> to the user. Is there any way to send the csv file without saving it to  
> the server? Or is there a command i can use to delete the file after it  
> sends? I couldnt quite find any answers to what I’m trying to do yet.  
> Thanks alot if anyone has input 🙂

Use FasterCSV.new instead.

```
csv_buffer = StringIO.new
FasterCSV.new(csv_buffer) do |fcsv|
    fcsv << row
end

send_buffer_to_user(csv_buffer.string)

```

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 8, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/best-way-for-sending-file-to-user/128461/4 "2008-02-08T17:08:44Z")

</div>

James G. wrote:

> Yes, if you can easily hold the data in memory. You could use  
> FasterCSV.generate() to get the data in a String. The use send\_data()  
> instead of send\_file() to dump it to the client.

Thanks, I used the generate option to get it to work, never used  
send\_data before so thanks for letting me know about that one 🙂
