Earlier in one of the articles we discussed how we can work with Uploadify, a Flash-based file uploader, along with Ruby on Rails 3. If you recall, Uploadify is actually a jQuery plugin which helps in uploading multiple files via a Flash-based interface. It gives pretty helpful functionality, for instance multi-upload queuing and progress bars apart from being extremely easy to use. In that article we delved into sending of the authentication token using the POST request and then passing the session cookie by adding it to the scriptData.

There is another aspect to that process which we need to consider. It is setting up of the controller and making it work with the upload. Hence, this post can even be considered as the second part of ‘Using Uploadify with Ruby on Rails 3’. So, let’s talk about:

The Controller

In the previous article on ‘Using Uploadify with Ruby on Rails 3’, we successfully uploaded our files and the authentication was also getting passed successfully all thanks to the work that we did there.

So, what next?

One of the many issues that you may run into is related to the content type of the file that you uploaded. The Uploadify upload that we did had content_type set as ‘application/octet-stream’ which you’ll notice (if you look closely) is not the exact representation of what we were trying to upload.
Please note, the example we’ll show here will make use of ‘paperclip gem’ for handling of the upload. It will require the actual content_type to be set accurately in order to continue with the file upload. We can make use of the mime-type gem and set the value for the content_type later in the controller before attempting to save the upload. This we’ll do for easy resolution of the ‘application/octet-stream’ issue. In order to do so, add the following to the gem file that you have, followed by running a bundle install:
gem ‘mime-types’ , :require => ‘mime/types’
Having this line in place, we’ll be able to force the controller to set a proper value for the upload’s content value. This can be achieved by using the following code:
params[:Filedata].content_type = MIME::Types.type_for(params[:Filedata].original_filename).to_s
We hope that you’ve found this post helpful for the multiple file upload task you were trying to accomplish.