package multipart

Import Path
	mime/multipart (on go.dev)

Dependency Relation
	imports 17 packages, and imported by one package

Involved Source Files formdata.go 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. # Limits To protect against malicious inputs, this package sets limits on the size of the MIME data it processes. Reader.NextPart and Reader.NextRawPart limit the number of headers in a part to 10000 and Reader.ReadForm limits the total number of headers in all FileHeaders to 10000. These limits may be adjusted with the GODEBUG=multipartmaxheaders=<values> setting. Reader.ReadForm further limits the number of parts in a form to 1000. This limit may be adjusted with the GODEBUG=multipartmaxparts=<value> setting. Copyright 2023 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. writer.go
Code Examples package main import ( "fmt" "io" "log" "mime" "mime/multipart" "net/mail" "strings" ) func main() { msg := &mail.Message{ Header: map[string][]string{ "Content-Type": {"multipart/mixed; boundary=foo"}, }, Body: strings.NewReader( "--foo\r\nFoo: one\r\n\r\nA section\r\n" + "--foo\r\nFoo: two\r\n\r\nAnd another\r\n" + "--foo--\r\n"), } mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) if err != nil { log.Fatal(err) } if strings.HasPrefix(mediaType, "multipart/") { mr := multipart.NewReader(msg.Body, params["boundary"]) for { p, err := mr.NextPart() if err == io.EOF { return } if err != nil { log.Fatal(err) } slurp, err := io.ReadAll(p) if err != nil { log.Fatal(err) } fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp) } } }
Package-Level Type Names (total 10, in which 6 are exported)
/* sort exporteds by: | */
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. ( File) Close() error ( File) Read(p []byte) (n int, err error) ( File) ReadAt(p []byte, off int64) (n int, err error) ( File) Seek(offset int64, whence int) (int64, error) *os.File File : io.Closer File : io.ReadCloser File : io.Reader File : io.ReaderAt File : io.ReadSeekCloser File : io.ReadSeeker File : io.Seeker func (*FileHeader).Open() (File, error) func net/http.(*Request).FormFile(key string) (File, *FileHeader, error)
A FileHeader describes a file part of a multipart request. Filename string Header textproto.MIMEHeader Size int64 Open opens and returns the FileHeader's associated File. func net/http.(*Request).FormFile(key string) (File, *FileHeader, error)
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. File map[string][]*FileHeader Value map[string][]string RemoveAll removes any temporary files associated with a Form. func (*Reader).ReadForm(maxMemory int64) (*Form, error)
A Part represents a single part in a multipart body. The headers of the body, if any, with the keys canonicalized in the same fashion that the Go http.Request headers are. For example, "foo-bar" changes case to "Foo-Bar" (*Part) Close() error FileName returns the filename parameter of the Part's Content-Disposition header. If not empty, the filename is passed through filepath.Base (which is platform dependent) before being returned. FormName returns the name parameter if p has a Content-Disposition of type "form-data". Otherwise it returns the empty string. Read reads the body of a part, after its headers and before the next part (if any) begins. *Part : io.Closer *Part : io.ReadCloser *Part : io.Reader func (*Reader).NextPart() (*Part, error) func (*Reader).NextRawPart() (*Part, error)
Reader is an iterator over parts in a MIME multipart body. Reader's underlying parser consumes its input as needed. Seeking isn't supported. NextPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned. As a special case, if the "Content-Transfer-Encoding" header has a value of "quoted-printable", that header is instead hidden and the body is transparently decoded during Read calls. NextRawPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned. Unlike NextPart, it does not have special handling for "Content-Transfer-Encoding: quoted-printable". 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. func NewReader(r io.Reader, boundary string) *Reader func net/http.(*Request).MultipartReader() (*Reader, error)
A Writer generates multipart messages. Boundary returns the Writer's boundary. Close finishes the multipart message and writes the trailing boundary end line to the output. CreateFormField calls CreatePart with a header using the given field name. CreateFormFile is a convenience wrapper around CreatePart. It creates a new form-data header with the provided field name and file name. 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. 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. WriteField calls CreateFormField and then writes the given value. *Writer : io.Closer func NewWriter(w io.Writer) *Writer
Package-Level Functions (total 11, in which 2 are exported)
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.
NewWriter returns a new multipart Writer with a random boundary, writing to w.
Package-Level Variables (total 6, in which 1 is exported)
ErrMessageTooLarge is returned by ReadForm if the message form data is too large to be processed.
Package-Level Constants (total 2, neither is exported)