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