Sending Multiple Files To The Browser

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 Stewart

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 does this for file restores).

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