views:

65

answers:

1

I need to allow multiple downloading of small documents in Rails, preferably using Paperclip (I've already used it to enable uploading).

Specific needs:

  • Zip the files for download.
  • Download different file types together (.jpeg, .doc, docx, .pdf).

I have found lots of tutorials online for multiple uploading, but not for downloads. I appreciate your help. Thanks!

A: 

Define a download action on the controller that is supposed to handle the download. The method could look something like this: (Given a File model with a paperclip attachment called attached)

def download
  require 'zip/zip'
  require 'zip/zipfilesystem'
  @files = File.all

  t = Tempfile.new('tmp-zip-' + request.remote_ip)
  Zip::ZipOutputStream.open(t.path) do |zos|
    @files.each do |file|
      zos.put_next_entry(file.attached_file_name)
      zos.print IO.read(file.attached.path)
    end
  end

  send_file t.path, :type => "application/zip", :filename => "Awesome.zip"

  t.close
end
Wolax