93b3a0d7ec
* feat(context): add SaveUploadedFile func. * feat(context): update multiple upload examples. * style(example): fix gofmt * fix(example): add missing return
36 lines
813 B
Go
36 lines
813 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
router := gin.Default()
|
|
router.Static("/", "./public")
|
|
router.POST("/upload", func(c *gin.Context) {
|
|
name := c.PostForm("name")
|
|
email := c.PostForm("email")
|
|
|
|
// Multipart form
|
|
form, err := c.MultipartForm()
|
|
if err != nil {
|
|
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
|
|
return
|
|
}
|
|
files := form.File["files"]
|
|
|
|
for _, file := range files {
|
|
if err := c.SaveUploadedFile(file, file.Filename); err != nil {
|
|
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
|
return
|
|
}
|
|
}
|
|
|
|
c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
|
|
})
|
|
router.Run(":8080")
|
|
}
|