Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file with Mechanize for Ruby

Tags:

ruby

mechanize

File upload does not work using:

form.file_upload_with(:name => 'image[1]').file_name = '/tmp/image.jpg'
form.submit

This is an out-of-date example: https://github.com/sparklemotion/mechanize/blob/master/examples/flickr_upload.rb

I tried this on two different sites.

I'm using Mechanize 2.6.0.

like image 889
Neschur Avatar asked Dec 26 '22 06:12

Neschur


1 Answers

Slightly off-topic, but another way to upload files with Mechanize I found useful, particularly if you don't have a HTML form handy, is to just use Mechanize.post with a File instance:

a = Mechanize.new
a.post(url, {
    "file1" => File.new("/tmp/image.jpg")
})
like image 184
lmika Avatar answered Jan 08 '23 18:01

lmika