Multipart/form-data, as the name implies, can upload multiple form-data and split it with separators for file uploads.
1. HTTP Multipart/form-data Request sample
Post/handle http/1.1
host:localhost:8080
Connection:keep-alive
content-length:182537
Cache-control:max-age=0
Content-type:multipart/form-data; boundary=----Webkitformboundarywddae6hxfa4nl2ig
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-encoding:gzip, deflate, BR
accept-language:zh-cn,zh;q=0.9
------Webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "Submit-name"
Jack
------Webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "File1"; Filename= "Out.png"
Content-type:image/png
Binary-data
------Webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "File2"; Filename= "2.png"
Content-type:image/png
Binary-data-2
------webkitformboundarywddae6hxfa4nl2ig--
2. Golang Parsing Example
Package Main
Import (
"Bytes"
"Net/http"
"Io/ioutil"
"FMT"
"Mime/multipart"
"IO"
)
Func Main () {
Bodybuf: = bytes. Newbuffer (Nil)
Bodybuf.writestring ('------webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "Submit-name"
Xiongwei
------Webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "File1"; Filename= "Out.png"
Content-type:image/png
Binary-data
------Webkitformboundarywddae6hxfa4nl2ig
Content-disposition:form-data; Name= "File2"; Filename= "2.png"
Content-type:image/png
Binary-data-2
------webkitformboundarywddae6hxfa4nl2ig--')
Req: = &http. request{
Method: "POST",
Header:http. header{"Content-type": {' multipart/form-data; boundary=----Webkitformboundarywddae6hxfa4nl2ig '}},
Body:ioutil. Nopcloser (BODYBUF),
}
GETMULTIPART3 (req)
}
Through R. Parsemultipartform
Func GetMultiPart1 (R *http. Request) {
/**
The underlying is parsed by calling Multipartreader.readform.
If the file size exceeds maxmemory, a temporary file is used to store the file data in the Multipart/form
*/
R.parsemultipartform (128)
Fmt. Println ("R.form:", R.form)
Fmt. Println ("R.postform:", R.postform)
Fmt. Println ("R.multipartform:", R.multipartform)
Getformdata (R.multipartform)
}
by Multipartreader
Func GetMultiPart2 (R *http. Request) () {
Mr,err: = R.multipartreader ()
If Err! = nil{
Fmt. Println ("R.multipartreader () Err,", err)
Return
}
form, _: = Mr. Readform (128)
Getformdata (Form)
}
BYTE parsing multi-part
Func GetMultiPart3 (R *http. Request) () {
Mr,err: = R.multipartreader ()
If Err! = nil{
Fmt. Println ("R.multipartreader () Err,", err)
Return
}
for{
P, Err: = Mr. Nextpart ()
If err = = Io. eof{
Break
}
If Err! = nil{
Fmt. Println ("Mr. Nextpart () Err, ", err)
Break
}
Fmt. Println ("Part header:", P.header)
FormName: = P.formname ()
FileName: = P.filename ()
If formName! = "" && fileName = = "" {
Formvalue,_:= Ioutil. ReadAll (P)
Fmt. Printf ("formname:%s,formvalue:%s\n", Formname,formvalue)
}
If fileName! = "" {
Filedata,_:=ioutil. ReadAll (P)
Fmt. Printf ("filename:%s,filedata:%s\n", Filename,filedata)
}
Fmt. Println ()
}
}
Func getformdata (Form *multipart. Form) {
Gets the form value in the Multi-part/form body
For k,v: = Range form. value{
Fmt. Println ("Value,k,v =", K, ",", V)
}
Fmt. Println ()
Get the file data in Multi-part/form
For _,v: = Range form. File {
for i:=0; i < Len (v); i++{
Fmt. Println ("File part", I, "--")
Fmt. Println ("FileName:", V[i]. Filename)
Fmt. Println ("Part-header:", V[i]. Header)
F,_: = V[i]. Open ()
Buf,_:= Ioutil. ReadAll (f)
Fmt. Println ("File-content", String (BUF))
Fmt. Println ()
}
}
}
Golang three ways to parse HTTP Multipart/form