Skip to content

Commit

Permalink
form add files
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaomingqiang committed Dec 12, 2020
1 parent c37c38e commit 86b5162
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
34 changes: 16 additions & 18 deletions api/core/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion api/core/request/formdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{},
Expand All @@ -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
Expand Down

0 comments on commit 86b5162

Please sign in to comment.