mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-25 03:12:16 +08:00
feat(context): update multiple upload examples.
This commit is contained in:
parent
f38df9dc19
commit
93191b14e0
@ -2,9 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -25,24 +23,9 @@ func main() {
|
|||||||
files := form.File["files"]
|
files := form.File["files"]
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
// Source
|
if err:=c.SaveUploadedFile(file,file.Filename);err!=nil{
|
||||||
src, err := file.Open()
|
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
||||||
if err != nil {
|
|
||||||
c.String(http.StatusBadRequest, fmt.Sprintf("file open 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))
|
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