File Uploader

Focus more on your domain not file upload. Let file_uploader do it.

Installation

gem install file_uploader

Usage in Rails

Open and edit config/environment.rb and add a line in Rails::Initializer.run do |config|

config.gem "file_uploader"

In your controller

class UploadController < ApplicationController

  def index
    if request.post?
      uploader = FileUploader.create # File is uploaded to public/upload directory
      uploader.save(params[:uploader][:file]) { |path|
        render :text => "#{path} is successfully saved."
      }
    end
  end

end

Upload to a different directory

uploader = FileUploader.create "public/a/b/c/d/e/f" # Directory hierarchy will be created and file will be saved to that location

More

Uploaded filename is sanitized and will have a random prefix automatically to prevent duplication.

FileUploader.save accept a block with path to the uploaded file.

Sinatra?

gist.github.com/799695