Robert
Robert

Reputation: 606

How do I generate multiple images and put them into one zip file for download using Rails3?

I am generating QR codes from Google's charting API on my website as a URL with some params passed in.

I have around 100 of these codes which are generated from a URL, something like this:

$= image_tag("http://chart.apis.google.com/chart?cht=qr&chl=#{qr.code}&chs=120x120&choe=UTF-8", :size => "120x120")

I want to create a method that loops through my array and generates the png files, then places them inside a zip file which I can download from one click.

I tried using send_data "url", :disposition = > "attachment", :type => "image/png"

This only saved the URL, not the image generated. Putting the URL into the browser opened a window with the image.

Other than that I was not able to add all the files into a zip file. Does Rails have its own built-in compression methods?

Upvotes: 3

Views: 1759

Answers (2)

Emiller
Emiller

Reputation: 1482

Check out my mod_zip module for Nginx:

https://github.com/evanmiller/mod_zip

You can dynamically stream a ZIP file to the client without any temporary files.

Upvotes: 1

Jonathan Tran
Jonathan Tran

Reputation: 15276

First, for each QR code, use Net::HTTP to download the QR code's image from Google's API into a temp file. Then use rubyzip to compress the temp files into a zip file. An example of how to use the zip library is here. Finally, use send_data (or send_file if you've written it to disk) to send this generated zip file to the client's web browser.

Upvotes: 5

Related Questions