mirror of
https://github.com/gin-gonic/gin.git
synced 2025-06-01 19:28:05 +08:00
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:
@ -2,9 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -25,24 +23,10 @@ func main() {
|
||||
files := form.File["files"]
|
||||
|
||||
for _, file := range files {
|
||||
// Source
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("file open err: %s", err.Error()))
|
||||
if err := c.SaveUploadedFile(file, file.Filename); err != nil {
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
// Destination
|
||||
dst, err := os.Create(file.Filename)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("Create file err: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
// Copy
|
||||
io.Copy(dst, src)
|
||||
}
|
||||
|
||||
c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
|
||||
|
Reference in New Issue
Block a user