mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
feat(context): update multiple upload examples.
This commit is contained in:
parent
f38df9dc19
commit
93191b14e0
@ -2,9 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -25,24 +23,9 @@ 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()))
|
||||
return
|
||||
if err:=c.SaveUploadedFile(file,file.Filename);err!=nil{
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
||||
}
|
||||
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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user