When sending files via POST (rfc1867), the HTTP message has a special content-type: multipart/form-data. Its body includes zero or more key/value parameters like in other POST messages, plus one or more file upload parameters. File upload parameters are special:
A single parameter may carry more than one file. This technique is called multipart/mixed.
Each file (not parameter!) can include extra metadata: its filename and mimetype.
This class models the file arguments used in multipart/form-data. See Net::HTTP::Post#set_multipart_data for examples.
Arguments:
The file to upload; either the pathname to open, or an IO object. If it's a pathname, it will be opened now and closed automatically later.
The content-type. Per RFC defaults to 'application/octet-stream', but I recommend setting it explicitly.
The file name to send to the remote server. If not supplied, will guess one based on the file's path. You can set it later with filename=.
# File multipart.rb, line 49 def initialize(filepath_or_io, mimetype='application/octet-stream', filename=nil) @mimetype = mimetype @filename = nil if filepath_or_io.respond_to? :read @input = filepath_or_io @shouldclose = false # came opened if filepath_or_io.respond_to? :path @filename = File.basename(filepath_or_io.path) end else @input = File.open(filepath_or_io, 'rb') @shouldclose = true # I opened it @filename = File.basename(filepath_or_io) end end
# File multipart.rb, line 75 def maybeclose @input.close if @shouldclose end
# File multipart.rb, line 71 def read(*args) @input.read(*args) end