?
This document uses PHP Chinese website manual Release
import "mime/multipart"
概述
索引
例子
Package multipart實現(xiàn)了RFC 2046中定義的MIME多部分解析。
對于HTTP(RFC 2388)和由流行瀏覽器生成的多部分機(jī)構(gòu)來說,這個實現(xiàn)就足夠了。
變量
type File
type FileHeader
func (fh *FileHeader) Open() (File, error)
type Form
func (f *Form) RemoveAll() error
type Part
func (p *Part) Close() error
func (p *Part) FileName() string
func (p *Part) FormName() string
func (p *Part) Read(d []byte) (n int, err error)
type Reader
func NewReader(r io.Reader, boundary string) *Reader
func (r *Reader) NextPart() (*Part, error)
func (r *Reader) ReadForm(maxMemory int64) (*Form, error)
type Writer
func NewWriter(w io.Writer) *Writer
func (w *Writer) Boundary() string
func (w *Writer) Close() error
func (w *Writer) CreateFormField(fieldname string) (io.Writer, error)
func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error)
func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error)
func (w *Writer) FormDataContentType() string
func (w *Writer) SetBoundary(boundary string) error
func (w *Writer) WriteField(fieldname, value string) error
NewReader
formdata.go multipart.go writer.go
如果消息表單數(shù)據(jù)太大而無法處理,則ErrMessageTooLarge由ReadForm返回。
var ErrMessageTooLarge = errors.New("multipart: message too large")
文件是訪問多部分消息的文件部分的接口。其內(nèi)容可以存儲在內(nèi)存中或磁盤上。如果存儲在磁盤上,則文件的基礎(chǔ)具體類型將是* os.File。
type File interface { io.Reader io.ReaderAt io.Seeker io.Closer}
FileHeader描述多部分請求的文件部分。
type FileHeader struct { Filename string Header textproto.MIMEHeader Size int64 // contains filtered or unexported fields}
func (fh *FileHeader) Open() (File, error)
打開并返回FileHeader的關(guān)聯(lián)文件。
表單是一個解析的多部分表單。它的File部分存儲在內(nèi)存中或磁盤上,并可通過* FileHeader的Open方法訪問。其Value部分以字符串形式存儲。兩者都以字段名稱為關(guān)鍵字。
type Form struct { Value map[string][]string File map[string][]*FileHeader}
func (f *Form) RemoveAll() error
RemoveAll刪除與表單關(guān)聯(lián)的所有臨時文件。
零件表示多部分機(jī)構(gòu)中的單個零件。
type Part struct { // 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" // // As a special case, if the "Content-Transfer-Encoding" header // has a value of "quoted-printable", that header is instead // hidden from this map and the body is transparently decoded // during Read calls. Header textproto.MIMEHeader // contains filtered or unexported fields}
func (p *Part) Close() error
func (p *Part) FileName() string
FileName返回零件的Content-Disposition標(biāo)題的文件名參數(shù)。
func (p *Part) FormName() string
如果p有一個類型為“form-data”的Content-Disposition,F(xiàn)ormName將返回name參數(shù)。否則,它返回空字符串。
func (p *Part) Read(d []byte) (n int, err error)
Read讀取零件的正文,在其標(biāo)題之后和下一部分(如果有)開始之前。
Reader是MIME多部分主體中的部分迭代器。Reader的底層解析器根據(jù)需要使用它的輸入。尋求不支持。
type Reader struct { // contains filtered or unexported fields}
func NewReader(r io.Reader, boundary string) *Reader
NewReader使用給定的MIME邊界從r創(chuàng)建新的多部分讀取器讀取。
邊界通常從消息的“Content-Type”頭部的“邊界”參數(shù)獲得。使用mime.ParseMediaType來解析這些標(biāo)題。
package mainimport ("fmt""io""io/ioutil""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 := ioutil.ReadAll(p)if err != nil { log.Fatal(err)} fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp)}}}
func (r *Reader) NextPart() (*Part, error)
NextPart返回多部分中的下一部分或錯誤。當(dāng)沒有更多的零件時,返回錯誤io.EOF。
func (r *Reader) ReadForm(maxMemory int64) (*Form, error)
ReadForm解析整個多部分消息,其部分具有“form-data”的內(nèi)容處置。它在內(nèi)存中存儲最大內(nèi)存字節(jié)數(shù)+ 10MB(為非文件部分保留)。無法存儲在內(nèi)存中的文件將以臨時文件的形式存儲在磁盤上。如果所有非文件部分都不能存儲在內(nèi)存中,它將返回ErrMessageTooLarge。
Writer生成多部分消息。
type Writer struct { // contains filtered or unexported fields}
func NewWriter(w io.Writer) *Writer
NewWriter返回一個新的具有隨機(jī)邊界的多部分寫入器,寫入w。
func (w *Writer) Boundary() string
Boundary返回 Writer的邊界。
func (w *Writer) Close() error
關(guān)閉完成多部分消息并將尾部邊界結(jié)束行寫入輸出。
func (w *Writer) CreateFormField(fieldname string) (io.Writer, error)
CreateFormField使用給定的字段名稱調(diào)用帶有標(biāo)題的CreatePart。
func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error)
CreateFormFile是CreatePart的一個便捷包裝。它使用提供的字段名稱和文件名創(chuàng)建一個新的表單數(shù)據(jù)標(biāo)題。
func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error)
CreatePart使用提供的標(biāo)題創(chuàng)建一個新的多部分部分。該部分的主體應(yīng)寫入返回的Writer。調(diào)用CreatePart之后,可能不再寫入任何以前的部分。
func (w *Writer) FormDataContentType() string
FormDataContentType返回具有該Writer邊界的HTTP multipart / form-data的Content-Type。
func (w *Writer) SetBoundary(boundary string) error
SetBoundary用顯式值覆蓋Writer的默認(rèn)隨機(jī)生成邊界分隔符。
必須在創(chuàng)建任何部件之前調(diào)用SetBoundary,可能只包含某些ASCII字符,并且必須非空且最多70個字節(jié)。
func (w *Writer) WriteField(fieldname, value string) error
WriteField調(diào)用CreateFormField,然后寫入給定的值。