Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:28:25+00:00 2026-05-17T20:28:25+00:00

I have paper_clip installed on my Rails 3 app, and can upload a file

  • 0

I have paper_clip installed on my Rails 3 app, and can upload a file – wow that was fun and easy!

Challenge now is, allowing a user to upload multiple objects.
Whether it be clicking select fileS and being able to select more than one. Or clicking a more button and getting another file upload button.

I can’t find any tutorials or gems to support this out of the box. Shocking I know…

Any suggestions or solutions. Seems like a common need?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:28:26+00:00Added an answer on May 17, 2026 at 8:28 pm

    Okay, this is a complex one but it is doable. Here’s how I got it to work.

    On the client side I used http://github.com/valums/file-uploader, a javascript library which allows multiple file uploads with progress-bar and drag-and-drop support. It’s well supported, highly configurable and the basic implementation is simple:

    In the view:

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

    In the js:

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

    When handed files, FileUploader posts them to the server as an XHR request where the POST body is the raw file data while the headers and filename are passed in the URL string (this is the only way to upload a file asyncronously via javascript).

    This is where it gets complicated, since Paperclip has no idea what to do with these raw requests, you have to catch and convert them back to standard files (preferably before they hit your Rails app), so that Paperclip can work it’s magic. This is done with some Rack Middleware which creates a new Tempfile (remember: Heroku is read only):

    # 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
    

    Later, in application.rb:

    config.middleware.use 'Rack::RawFileStubber'
    

    Then in the controller:

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

    This works reliably, though it can be a slow process when uploading a lot of files simultaneously.

    DISCLAIMER

    This was implemented for a project with a single, known & trusted back-end user. It almost certainly has some serious performance implications for a high traffic Heroku app and I have not fire tested it for security. That said, it definitely works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses the Paperclip plugin for image upload. Now that
I have a Rails app that needs to store resumes for job applications. Locally,
I have installed paperclip on my rails app (deployed on heroku). On my localhost
I just installed paper_clip to allow user's to upload a profile pic which can
Hey i have a rails app that i am uploading images with paperclip.... So
I have a RESTful resource in my Rails app called Photo. I'm using Paperclip
I'm trying to add photos to my app using paperclip. A mug can have
I have a rails 2.3.5 app and if I try to do rake db:migrate,
I just successfully installed WysiHat in my rails blog. Seems that the 'add a
I have just installed paperclip into my ruby on rails blog application. Everything is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.