Rails - Paper_Clip - Support for multi-file downloads

I have paper_clip installed in my Rails 3 application and you can download the file - it was fun and easy!

Now a call that allows the user to load multiple objects. Will you click select filesS and be able to select more than one. Or click the more button and click the add file button.

I cannot find any textbooks or gems to support this out of the box. Shocking, I know ...

Any suggestions or solutions. Sounds like a general need?

thank

+5
source share
3 answers

Well, it’s difficult, but it’s doable. This is how I got it to work.

http://github.com/valums/file-uploader, javascript-, . , :

:

<div id='file-uploader'><noscript><p>Please Enable JavaScript to use the file uploader</p></noscript></div>

js:

var uploader = new qq.FileUploader({
   element: $('#file-uploader')[0],
   action: 'files/upload',
   onComplete: function(id, fileName, responseJSON){
     // callback
   }
});

FileUploader XHR, POST , URL ( javascript).

, Paperclip , , (, Rails), Paperclip . Rack, Tempfile (: Heroku ):

# Embarrassing note: This code was adapted from an example I found somewhere online
# if you recoginize any of it please let me know so I pass credit.
module Rack
  class RawFileStubber

    def initialize(app, path=/files\/upload/) # change for your route, careful.
      @app, @path = app, path
    end

    def call(env)
      if env["PATH_INFO"] =~ @path
        convert_and_pass_on(env)
      end
      @app.call(env)
    end

    def convert_and_pass_on(env)
      tempfile = env['rack.input'].to_tempfile      
      fake_file = {
        :filename => env['HTTP_X_FILE_NAME'],
        :type => content_type(env['HTTP_X_FILE_NAME']),
        :tempfile => tempfile
      }
      env['rack.request.form_input'] = env['rack.input']
      env['rack.request.form_hash'] ||= {}
      env['rack.request.query_hash'] ||= {}
      env['rack.request.form_hash']['file'] = fake_file
      env['rack.request.query_hash']['file'] = fake_file
      if query_params = env['HTTP_X_QUERY_PARAMS']
        require 'json'
        params = JSON.parse(query_params)
        env['rack.request.form_hash'].merge!(params)
        env['rack.request.query_hash'].merge!(params)
      end
    end

    def content_type(filename)
      case type = (filename.to_s.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
      when %r"jp(e|g|eg)"            then "image/jpeg"
      when %r"tiff?"                 then "image/tiff"
      when %r"png", "gif", "bmp"     then "image/#{type}"
      when "txt"                     then "text/plain"
      when %r"html?"                 then "text/html"
      when "js"                      then "application/js"
      when "csv", "xml", "css"       then "text/#{type}"
      else 'application/octet-stream'
      end
    end
  end
end

application.rb:

config.middleware.use 'Rack::RawFileStubber'

:

  def upload
    @foo = modelWithPaperclip.create({ :img => params[:file] })
  end

, .

, . Heroku, . , .

+5

Rails 3 8. S3 .

. , , , . , , , Rails . , , .

, Paperclip . , SO, /.

S3, :

has_attached_file :photo, :styles => { ... }, :storage => :s3

You need to tune Paperclip::Storage::S3S3 with your data to tune it, and again Paperclip got some pretty awesome documentation for this.

Good luck

0
source

All Articles