Overview

Package multipart implements MIME multipart parsing, as defined in RFC 2046.

The implementation is sufficient for HTTP (RFC 2388) and the multipart bodies
generated by popular browsers.

Index

Package files

formdata.go writer.go

ErrMessageTooLarge is returned by ReadForm if the message form data is too large
to be processed.

type

  1. type File interface {
  2. .Reader
  3. .ReaderAt
  4. .Seeker
  5. .Closer
  6. }

File is an interface to access the file part of a multipart message. Its
contents may be either stored in memory or on disk. If stored on disk, the
File’s underlying concrete type will be an *os.File.

type

  1. Filename
  2. Header textproto.
  3. Size int64
  4. // contains filtered or unexported fields
  5. }

A FileHeader describes a file part of a multipart request.

func (*FileHeader)

  1. func (fh *) Open() (File, )

Open opens and returns the FileHeader’s associated File.

  1. type Form struct {
  2. Value map[string][]
  3. File map[string][]*
  4. }

Form is a parsed multipart form. Its File parts are stored either in memory or
on disk, and are accessible via the *FileHeader’s Open method. Its Value parts
are stored as strings. Both are keyed by field name.

func (*Form) RemoveAll

  1. func (f *Form) RemoveAll()

RemoveAll removes any temporary files associated with a Form.

type Part

  1. type Part struct {
  2. // The headers of the body, if any, with the keys canonicalized
  3. // in the same fashion that the Go http.Request headers are.
  4. // For example, "foo-bar" changes case to "Foo-Bar"
  5. //
  6. // As a special case, if the "Content-Transfer-Encoding" header
  7. // has a value of "quoted-printable", that header is instead
  8. // hidden from this map and the body is transparently decoded
  9. // during Read calls.
  10. Header textproto.
  11. // contains filtered or unexported fields
  12. }

A Part represents a single part in a multipart body.

func (*Part) Close

  1. func (p *Part) Close()

func (*Part) FileName

FileName returns the filename parameter of the Part’s Content-Disposition
header.

  1. func (p *Part) FormName()

FormName returns the name parameter if p has a Content-Disposition of type
“form-data”. Otherwise it returns the empty string.

func (*Part) Read

  1. func (p *Part) Read(d []) (n int, err )

Read reads the body of a part, after its headers and before the next part (if
any) begins.

type Reader

  1. type Reader struct {
  2. // contains filtered or unexported fields
  3. }

Reader is an iterator over parts in a MIME multipart body. Reader’s underlying
parser consumes its input as needed. Seeking isn’t supported.

func NewReader

  1. func NewReader(r io., boundary string) *

NewReader creates a new multipart Reader reading from r using the given MIME
boundary.

The boundary is usually obtained from the “boundary” parameter of the message’s
“Content-Type” header. Use mime.ParseMediaType to parse such headers.


Example:

  1. msg := &mail.Message{
  2. Header: map[string][]string{
  3. "Content-Type": {"multipart/mixed; boundary=foo"},
  4. },
  5. Body: strings.NewReader(
  6. "--foo\r\nFoo: one\r\n\r\nA section\r\n" +
  7. "--foo--\r\n"),
  8. }
  9. mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
  10. log.Fatal(err)
  11. }
  12. if strings.HasPrefix(mediaType, "multipart/") {
  13. mr := multipart.NewReader(msg.Body, params["boundary"])
  14. for {
  15. p, err := mr.NextPart()
  16. if err == io.EOF {
  17. return
  18. }
  19. if err != nil {
  20. }
  21. slurp, err := ioutil.ReadAll(p)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp)
  26. }
  27. }
  28. // Output:
  29. // Part "two": "And another"

func (*Reader)

  1. func (r *) NextPart() (*Part, )

NextPart returns the next part in the multipart or an error. When there are no
more parts, the error io.EOF is returned.

func (*Reader) ReadForm

  1. func (r *Reader) ReadForm(maxMemory ) (*Form, )

ReadForm parses an entire multipart message whose parts have a
Content-Disposition of “form-data”. It stores up to maxMemory bytes + 10MB
(reserved for non-file parts) in memory. File parts which can’t be stored in
memory will be stored on disk in temporary files. It returns ErrMessageTooLarge
if all non-file parts can’t be stored in memory.

A Writer generates multipart messages.

func NewWriter

  1. func NewWriter(w io.) *Writer

NewWriter returns a new multipart Writer with a random boundary, writing to w.

  1. func (w *) Boundary() string

Boundary returns the Writer’s boundary.

func (*Writer)

  1. func (w *) Close() error

Close finishes the multipart message and writes the trailing boundary end line
to the output.

func (*Writer)

  1. func (w *) CreateFormField(fieldname string) (.Writer, )

CreateFormField calls CreatePart with a header using the given field name.

func (*Writer) CreateFormFile

  1. func (w *Writer) CreateFormFile(fieldname, filename ) (io., error)

CreateFormFile is a convenience wrapper around CreatePart. It creates a new
form-data header with the provided field name and file name.

func (*Writer)

  1. func (w *) CreatePart(header textproto.) (io., error)

CreatePart creates a new multipart section with the provided header. The body of
the part should be written to the returned Writer. After calling CreatePart, any
previous part may no longer be written to.

func (*Writer)

  1. func (w *) FormDataContentType() string

FormDataContentType returns the Content-Type for an HTTP multipart/form-data
with this Writer’s Boundary.

SetBoundary overrides the Writer’s default randomly-generated boundary separator
with an explicit value.

SetBoundary must be called before any parts are created, may only contain
certain ASCII characters, and must be non-empty and at most 70 bytes long.

func (*Writer)

    WriteField calls CreateFormField and then writes the given value.