feat(context): add SaveUploadedFile func. (#1022)

* feat(context): add SaveUploadedFile func.

* feat(context): update multiple upload examples.

* style(example): fix gofmt

* fix(example): add missing return
This commit is contained in:
Eason Lin
2017-07-16 11:42:08 +08:00
committed by Bo-Yi Wu
parent 5cb25a6410
commit 93b3a0d7ec
5 changed files with 73 additions and 36 deletions

View File

@ -13,6 +13,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
@ -431,6 +432,24 @@ func (c *Context) MultipartForm() (*multipart.Form, error) {
return c.Request.MultipartForm, err
}
// SaveUploadedFile uploads the form file to specific dst.
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error {
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
io.Copy(out, src)
return nil
}
// Bind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding