# Sending Multiple Files To The Browser

**URL:** <https://www.ruby-forum.com/t/sending-multiple-files-to-the-browser/111385>\
**Category:** Rails\
**Created:** [August 6, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/sending-multiple-files-to-the-browser/111385 "2007-08-06T18:54:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [August 6, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/sending-multiple-files-to-the-browser/111385/1 "2007-08-06T18:54:50Z")

</div>

Hello,

I am having trouble sending multiple files to the browser in one  
action. I can’t be the first to do this, so I’m wondering what the  
best pattern is.

The user has a screen where they can choose files they would like to  
download (typically from one to ten or so). Next they click a button  
which submits the form to my action. My action extracts the ids of  
the models from the request and then downloads the files from S3  
(where they were stored with attachment\_fu).

Here’s the code in my action:

def download  
Asset.find\_all\_by\_id(params[:asset\_ids]).each do |asset|  
open(File.join(‘private’, asset.filename), ‘w’) do |file|  
AWS::S3::S3Object.stream(asset.full\_filename,  
asset.bucket\_name) do |chunk|  
file.write chunk  
end  
end  
#send\_file File.join(‘private’, asset.filename)  
end  
end

I download each file in turn from S3 and save it onto disk. Ideally  
I’d like then to send all the files to the browser (as attachments)  
so the browser downloads them all to the user’s disk. But I’m only  
able to send one of the files this way. Is there a way to send many  
files like this?

(I suppose if I can only send a single file to the browser I’ll have  
to zip them up into one file and send that.)

As an aside, I’m writing the files to a temporary area on disk before  
sending them to the browser. I’d rather just stream them directly  
from S3 to the browser – is that possible instead?

Thanks in advance,

Andy S.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [August 6, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/sending-multiple-files-to-the-browser/111385/2 "2007-08-06T19:12:51Z")

</div>

You’ll have to zip them up before you send them. One approach a lot of  
sites  
use is a batching approach, where you request your files and then the  
site  
sends you a download link when they’re ready. ([mozy.com](http://mozy.com) does this for  
file  
restores).

But yeah, the way the protocol works is by one response at a time.
