views:

2406

answers:

3

I have a web application that needs to take a file upload from the user and upload it to a remote server. I can take input from user to server fine via file_field, but can't seem to work out the next step of uploading from server to remote. Net::HTTP doesn't do multipart forms out of the box, and I haven't been able to find another good solution. I need something that will allow me to go from user -> server -> remote instead of going user -> remote. Anyone succeeded in doing this before?

+1  A: 

Surprisingly multipart form posts really aren't in Net:HTTP. A thread from comp.lang.ruby seems to have snippet of code you might find useful to perform the encoding necessary:

BOUNDARY = "AaB03x"

def encode_multipartformdata(parameters = {})
  ret = String.new
  parameters.each do |key, value|
    unless value.empty?
      ret << "\r\n--" << BOUNDARY << "\r\n"
      ret << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
      ret << value
    end
  end
  ret << "\r\n--" << BOUNDARY << "--\r\n"
end
ctcherry
That looks like it'll do it - I'll try it in a bit and let you know if it works.
Misplaced
A: 

Agile Web Development with Rails has an example on exactly this problem in chapter 22.

Otto
This is more of an advertisement than help, isn't it?
fixermark
+2  A: 

I believe the attachment_fu plugin would allow for this:

http://svn.techno-weenie.net/projects/plugins/attachment_fu/

HTH,

Nick

NickR