#399 new
Jarl Friis

Webrat does not support multiple file upload

Reported by Jarl Friis | October 28th, 2010 @ 05:28 AM

Firefox, Chrome and HTML5 let you have a file field like this:

<input type="file" name="myarray[]" multiple/>

That will trigger the browser to select multiple files.
However Webrat does not support testing this feature.

To mske it work (on server side) the name must end with "[]" indicating an array value is expected.

I have made an ugly monkey patch (against 0.7.1) to make things work, here it is:

module Webrat
  class FileField
    def test_uploaded_file
      return "" if @original_value.blank?

      case Webrat.configuration.mode
      when :rails
        if Array === @original_value  ###THIS IS NEW
          @original_value.enum_for(:each_with_index).collect do |ov, i|
            if content_type[i]
              ActionController::TestUploadedFile.new(@original_value[i], content_type[i])
            else
              ActionController::TestUploadedFile.new(@original_value[i])
            end
          end
        else
          if content_type
            ActionController::TestUploadedFile.new(@original_value, content_type)
          else
            ActionController::TestUploadedFile.new(@original_value)
          end
        end
      end
    end
  end

  class Form
    protected

    def self.replace_param_value(params, oval, nval)
      output = Hash.new
      params.each do |key, value|
        case value
        when Hash
          value = replace_param_value(value, oval, nval)
        when Array
          value = value.map { |o| o == oval ? nval : ( o.is_a?(Hash) ? replace_param_value(o, oval, nval) : o) }
          value.flatten!(1) if Array === nval ###THIS IS NEW
        when oval
          value = nval
        end
        output[key] = value
      end
      output
    end
  end
end

Hope this will be part of webrat.

Comments and changes to this ticket

  • Chris Hapgood

    Chris Hapgood December 30th, 2010 @ 05:07 PM

    Beware that Rails (2.3.5 and perhaps others) appear to have a more fundamental problem with accepting file parameters in an array.

    For example, this results in severe ugliness in Rails:

    post gallery_attachments_path(galleries(:one).id), :attachment => [{:description => "The photo", :file => fixture_file_upload('a/plus.png', Mime::PNG)}]

    The hash representing the first (and only) array element is munged into a single string, probably somewhere in ActionController::TestProcess' parameter handling. Note that this works fine:

    post gallery_attachments_path(galleries(:one).id), :attachment => {:description => "The photo", :file => fixture_file_upload('a/plus.png', Mime::PNG)}

    So I would not be surprised if the bug reported on this ticket is actually a Rails bug.

  • Jarl Friis

    Jarl Friis January 4th, 2011 @ 07:59 AM

    • Tag changed from attach_file, file_field to attach_file, file_field, multiple

    I don't know about rails 2.3.5 but for rails 2.3.9 I can say that I have a view (new.html.erb) with

    f.file_field(:photo_files, :multiple => true)
    

    that generates request data (to the create method)
    {"photo_files"=>[#<File:/tmp/RackMultipart20110104-12495-1dpl7x-0>, #<File:/tmp/RackMultipart20110104-12495-q4219h-0>]}

    Which at least is what I expect from Rails, so I don't think there is anything wrong with Rails.

    I then test my create method like this:

    post :create, {
        :photo_files => 
        [fixture_file_upload('files/photo.jpg', 'image/jpeg'),
         fixture_file_upload('files/photo.jpg', 'image/jpeg'),
         ]
      }
    

    All this is expected from Rails. I only see a problem with webrat.

  • Chris Hapgood

    Chris Hapgood May 13th, 2011 @ 01:13 PM

    After a recent upgrade to Rails 2.3.11, I confirm what Jarl notes: Rails behaves properly. I conclude that uploading a file into an array parameter was broken in 2.3.5 and is now fixed (in Integration testing).

    Furthermore, Webrat 0.7.2 seems to trigger the upload correctly.

  • Jarl Friis

    Jarl Friis October 30th, 2012 @ 04:23 AM

    • Tag changed from attach_file, file_field, multiple to multiple, attach_file, file_field

    If anyone ever finds this and is interesting in the feature. I think you should know that this has been fixed in capybara:
    https://github.com/jnicklas/capybara/commit/3e2d7c2114168756a14cdba...

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Pages