2017-01-03 03:50:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
2017-09-07 03:45:16 +00:00
|
|
|
// Set a lower memory limit for multipart forms (default is 32 MiB)
|
|
|
|
router.MaxMultipartMemory = 8 << 20 // 8 MiB
|
2017-01-03 03:50:35 +00:00
|
|
|
router.Static("/", "./public")
|
|
|
|
router.POST("/upload", func(c *gin.Context) {
|
|
|
|
name := c.PostForm("name")
|
|
|
|
email := c.PostForm("email")
|
|
|
|
|
|
|
|
// Multipart form
|
2017-07-04 08:12:29 +00:00
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2017-01-03 03:50:35 +00:00
|
|
|
files := form.File["files"]
|
|
|
|
|
|
|
|
for _, file := range files {
|
2017-07-16 03:42:08 +00:00
|
|
|
if err := c.SaveUploadedFile(file, file.Filename); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
2017-07-04 08:12:29 +00:00
|
|
|
return
|
|
|
|
}
|
2017-01-03 03:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
|
|
|
|
})
|
|
|
|
router.Run(":8080")
|
|
|
|
}
|