Skip to content

Instantly share code, notes, and snippets.

@AmaranthLIS
Created October 15, 2018 12:36
Show Gist options
  • Save AmaranthLIS/6eb51d5cd1984d479e4c77d2abb48109 to your computer and use it in GitHub Desktop.
Save AmaranthLIS/6eb51d5cd1984d479e4c77d2abb48109 to your computer and use it in GitHub Desktop.
Receive file with Gin
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
// router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.Static("/file", "saved")
router.POST("/many", func(c *gin.Context) {
// Multipart form
form, _ := c.MultipartForm()
files := form.File["file"]
for _, file := range files {
log.Println(file.Filename)
err := c.SaveUploadedFile(file, "saved/"+file.Filename)
if err != nil {
log.Fatal(err)
}
}
fmt.Println(c.PostForm("key"))
c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
})
router.POST("/one", func(c *gin.Context) {
// single file
file, err := c.FormFile("file")
if err != nil {
log.Fatal(err)
}
log.Println(file.Filename)
err = c.SaveUploadedFile(file, "saved/"+file.Filename)
if err != nil {
log.Fatal(err)
}
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
router.Run(":8001")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment