Search code examples
rubyfile-uploadmechanizemechanize-ruby

Use Ruby Mechanize to simulate file uploading by Flash


I want to use Ruby Mechanize to write an auto file uploading script for a website. But the read upload step of the website is done by a Flash file. I use fiddler captured the traffic from the Flash file to the server. Here is it.

POST /upload.php HTTP/1.1
Accept: text/*
Content-Type: multipart/form-data; boundary=----------KM7KM7Ij5KM7KM7ei4GI3GI3gL6GI3
User-Agent: Shockwave Flash
Host: 119.147.15.17<this is fake IP address>
Content-Length: 7800236
Connection: Keep-Alive
Pragma: no-cache

------------KM7KM7Ij5KM7KM7ei4GI3GI3gL6GI3
Content-Disposition: form-data; name="Filename"

learning_objectives.flv
------------KM7KM7Ij5KM7KM7ei4GI3GI3gL6GI3
Content-Disposition: form-data; name="FileData"; filename="learning_objectives.flv"
Content-Type: application/octet-stream

This_is_the_real_data_in_learning_objectives.flv

My question is how can I simulate this post requrest? Currently what I can think of is create a form by myself and add an input element with type file. Then submit this form with customized headers (such as the user-agent etc.). But I'm still have difficult in dealing with the file upload part. Anyone can help me with it? Thanks!


Solution

  • It should look something like this:

    agent.post url, 'FileData' => File.new('learning_objectives.flv')