For my Ruby on Rails project (Rails version 5.1.2), I'm generating image files (png) and downloading them as a zipfile using RubyZip gem.
The image files are not stored in any directory. I have a model called Attachment. Each attachment has an attribute image_string that is a base64 string for an image. You can show the images using a tag like image_tag(src = "data:image/jpeg;base64, #{attachment.image_string}", style: "border-radius: 0;")
For multiple images, I want to create temporary file for each of them without storing them anywhere and download those images as a zip file.
The code I have now:
def bulk_download
require('zip')
::Zip::File.open("/tmp/mms.zip", Zip::File::CREATE) do |zipfile|
Attachment.all.each do |attachment|
image_file = Tempfile.new("#{attachment.created_at.in_time_zone}.png")
image_file.write(attachment.image_string)
zipfile.add("#{attachment.created_at.in_time_zone}.png", image_file.path)
end
end
send_file "/tmp/mms.zip", type: 'application/zip', disposition: 'attachment', filename: "my_archive.zip"
respond_to do |format |
format.all { head :ok, content_type: "text/html" }
end
end
But the downloaded zipfile has no files in it and the size of it is 0 bytes. Thanks in advance.