class Net::HTTP::FileForPost

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:

This class models the file arguments used in multipart/form-data. See Net::HTTP::Post#set_multipart_data for examples.

Attributes

filename[RW]
input[R]
mimetype[RW]
shouldclose[RW]

Public Class Methods

new(filepath_or_io, mimetype='application/octet-stream', filename=nil) click to toggle source

Arguments:

filepath_or_io

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.

mimetype

The content-type. Per RFC defaults to 'application/octet-stream', but I recommend setting it explicitly.

filename

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

Public Instance Methods

maybeclose() click to toggle source
# File multipart.rb, line 75
def maybeclose
  @input.close if @shouldclose
end
read(*args) click to toggle source
# File multipart.rb, line 71
def read(*args)
  @input.read(*args)
end