diff --git a/api/core/handlers/handlers.go b/api/core/handlers/handlers.go index 1bc50788..adf78839 100644 --- a/api/core/handlers/handlers.go +++ b/api/core/handlers/handlers.go @@ -365,24 +365,22 @@ func reqBodyFromFormData(_ *core.Context, req *request.Request) { } writer := multipart.NewWriter(reqBody) for key, val := range fd.Params() { - switch v := val.(type) { - case *request.File: - part, err := writer.CreatePart(v.MIMEHeader()) - if err != nil { - req.Err = err - return - } - _, err = io.Copy(part, v) - if err != nil { - req.Err = err - return - } - default: - err := writer.WriteField(key, fmt.Sprint(val)) - if err != nil { - req.Err = err - return - } + err := writer.WriteField(key, fmt.Sprint(val)) + if err != nil { + req.Err = err + return + } + } + for _, file := range fd.Files() { + part, err := writer.CreatePart(file.MIMEHeader()) + if err != nil { + req.Err = err + return + } + _, err = io.Copy(part, file) + if err != nil { + req.Err = err + return } } req.ContentType = writer.FormDataContentType() diff --git a/api/core/request/formdata.go b/api/core/request/formdata.go index ddf73eaf..667688b6 100644 --- a/api/core/request/formdata.go +++ b/api/core/request/formdata.go @@ -17,6 +17,10 @@ func (fd *FormData) Params() map[string]interface{} { return fd.params } +func (fd *FormData) Files() []*File { + return fd.files +} + func NewFormData() *FormData { return &FormData{ params: map[string]interface{}{}, @@ -29,7 +33,6 @@ func (fd *FormData) AddParam(field string, val interface{}) *FormData { } func (fd *FormData) AddFile(field string, file *File) *FormData { - fd.params[field] = file file.fieldName = field fd.files = append(fd.files, file) return fd