Uploadify is an javacript library to upload multiple files with neat progress bars and such. UploadifyRails allows you to easily get Uploadify up and going without headaches. This plugin solves the major problems in getting Uploadify running in rails. These problems include getting the Authentication Token and Session ID into the request sent by the flash object. Also, the request format is set to JSON, so you can get set some sort of response when the file(s) is properly uploaded.
Listed below are the links to various dependencies and the github repository of this plugin.
First, the script/plugin installer should’ve added the supported uploadify js library and it’s dependency (swfobject.js). You’ll need to have JQuery and JQueryUI already installed.
If the installer didn’t add the files you can also run this rake task:
rake uploadify_rails:install
Next, you’ll need to modify config/initializers/session_store.rb so that it will insert the provided middleware. Modify the ActionController::Dispatcher so that the FlashSessionCookieMiddleware is in-between the cookie store and the base session like this:
# config/initializers/session_store.rb ActionController::Base.session = { :key => '_uploadify_rails_session', :secret => 'woot!' } ActionController::Dispatcher.middleware.insert_before( ActionController::Session::CookieStore, FlashSessionCookieMiddleware, # <-- here is where it goes! ActionController::Base.session_options[:key] )
Flash doesn’t send the MIME type in the octet stream when uploading. You’ll need to use something like the mime-types gem in order to set the MIME type of the file correctly.
To install the mime-types gem do the following:
sudo gem install mime-types
Then in the environment.rb add this to initializer config:
# config/environment.rb config.gem 'mime-types', :lib => "mime/types"
Finally, either in some sort of Model callback or within the controller method you’re using to create the file, make sure to set the MIME type using the filename. For example:
@photo.photo_content_type = MIME::Types.type_for(@photo.photo_file_name).to_s
Once installed, you’ll have some nifty helper methods to easily add Uploadify.
Use the render_uploadify method to create the uploadify js and load the appropriate js files. You can pass in several options into this method. You’ll need to enter in at least the url of the path you’re uploading the files to, and the id of the file field you are working with. You will also have a couple helper methods to submit the uploadify file fields and to cancel any queued uploads.
Refer to the code for the default options and what you options you can pass into the methods.
Lets say you want to post the files to a photos controller, and you have a file_field with an id “photo_photo”:
<%= render_uploadify(:id => "photo_photo", :url => photos_path, :allow_multiple_files => false) %> <div> <%= file_field :photo, :photo %> </div> <div id="photo_submit"> <%= uploadify_submit("Upload") %> <%= uploadify_cancel("Stop") %> </div>
Thanks to github.com/leeh for the uploadify_paperclip_demo of which this plugin is based on.
Copyright © 2010 Alain Bloch, released under the MIT license