Tyche asked:

How do I access multipart data?

I want to the user to be able to be able to upload zip/tar/jpg files. How do I access multipart data in Nitro’s request? Are there any handy tags/tools in Nitro’s library for generating multipart input forms? Is there anything in the Ajax interface that is useful for this?

(2 attempts)

manveru answered:

I was going to post this on the Mailinglist, but now that i see your question here, it might be of more use to more people :)

We've done quite some work on the file-uploading, nothing ajax-related though, shouldn't be hard to do using prototype/script.aculo.us - you could check their wiki if you want to learn more.

I will give you an example on how to upload images, produce thumbnails for them and save them, just in a few lines of code... if you need more information on that, please ask in the IRC-channel where i usually idle around anyway, here your answer :)

template:

<form action="save" method="post" enctype="multipart/form-data" class="shown" id="upload">
<input type="file" name="file" /><br />
Title: <input type="text" name="title" />
<input type="submit" />
</form>

controller:

class MyController
  def save
    if request.post?
      picture = Picture.assign(request)
      if picture.save
        @out << 'success'
      else
        @out << "failed: #{errors.inspect}"
      end
    end
  end
end

model:

require_gem 'rmagick'
require 'glue/webfile'

class Picture
  property :title, String
  property :file, WebFile, :magick => { :small => '150x125', :medium => '600x500' }
end

that's it... more or less you should be able to figure the rest out yourself, it's really not too hard

basically the server first uploads the file to /tmp where you get a file-handle on it (that is what request['file'] will contain in the controller, go and check it... will help you quite a bit with understanding)

the problem with the current approach is only that it's usable only for images, now you can use it for usual files, by just not using the :magick option. there are some tricks for assigning other filenames too, but it's a quite bumpy road, you better read the source of webfile.rb for that.

hope it helps :)

Rating: 4

Fabian answered:

<!-- form with virtual method (:multipart),
       special controls (:select_file) -->

  <div id="myform">
  #{form :method => :multipart do |f|
    f.p {
      f.label 'Select the new icon filename'
      f.select_file :file, :label => 'Select icon'
    }
    f.p {
      f.submit 'Change'
    }
  end}
  </div>

Which probably works only with Nitro version > 0.30.0.

Rating: 3