mirror of https://github.com/gin-gonic/gin.git
Added support multipart/form-data #109
This commit is contained in:
parent
0f46ae2b81
commit
0fb7bed1c0
|
@ -25,14 +25,20 @@ type (
|
||||||
// XML binding
|
// XML binding
|
||||||
xmlBinding struct{}
|
xmlBinding struct{}
|
||||||
|
|
||||||
// // form binding
|
// form binding
|
||||||
formBinding struct{}
|
formBinding struct{}
|
||||||
|
|
||||||
|
// multipart form binding
|
||||||
|
multipartFormBinding struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MAX_MEMORY = 1 * 1024 * 1024
|
||||||
|
|
||||||
var (
|
var (
|
||||||
JSON = jsonBinding{}
|
JSON = jsonBinding{}
|
||||||
XML = xmlBinding{}
|
XML = xmlBinding{}
|
||||||
Form = formBinding{} // todo
|
Form = formBinding{} // todo
|
||||||
|
MultipartForm = multipartFormBinding{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
@ -63,6 +69,16 @@ func (_ formBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
return Validate(obj)
|
return Validate(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_ multipartFormBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
|
if err := req.ParseMultipartForm(MAX_MEMORY); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mapForm(obj, req.Form); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return Validate(obj)
|
||||||
|
}
|
||||||
|
|
||||||
func mapForm(ptr interface{}, form map[string][]string) error {
|
func mapForm(ptr interface{}, form map[string][]string) error {
|
||||||
typ := reflect.TypeOf(ptr).Elem()
|
typ := reflect.TypeOf(ptr).Elem()
|
||||||
formStruct := reflect.ValueOf(ptr).Elem()
|
formStruct := reflect.ValueOf(ptr).Elem()
|
||||||
|
|
|
@ -295,6 +295,8 @@ func (c *Context) Bind(obj interface{}) bool {
|
||||||
switch {
|
switch {
|
||||||
case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
|
case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
|
||||||
b = binding.Form
|
b = binding.Form
|
||||||
|
case ctype == MIMEMultipartPOSTForm:
|
||||||
|
b = binding.MultipartForm
|
||||||
case ctype == MIMEJSON:
|
case ctype == MIMEJSON:
|
||||||
b = binding.JSON
|
b = binding.JSON
|
||||||
case ctype == MIMEXML || ctype == MIMEXML2:
|
case ctype == MIMEXML || ctype == MIMEXML2:
|
||||||
|
|
1
gin.go
1
gin.go
|
@ -21,6 +21,7 @@ const (
|
||||||
MIMEXML2 = "text/xml"
|
MIMEXML2 = "text/xml"
|
||||||
MIMEPlain = "text/plain"
|
MIMEPlain = "text/plain"
|
||||||
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
MIMEPOSTForm = "application/x-www-form-urlencoded"
|
||||||
|
MIMEMultipartPOSTForm = "multipart/form-data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|
Loading…
Reference in New Issue