0%

Gin 的 上传与下载

Gin 的上传

直接看官方示例就可以 - https://gin-gonic.com/docs/examples/upload-file/multiple-file/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.POST("/upload", func(c *gin.Context) {
// Multipart form
form, _ := c.MultipartForm()
files := form.File["upload[]"]

for _, file := range files {
log.Println(file.Filename)

// Upload the file to specific dst.
c.SaveUploadedFile(file, dst)
}
c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
})
router.Run(":8080")
}

Gin 的 下载

一个是静态文件服务 - https://gin-gonic.com/zh-cn/docs/examples/serving-static-files/
另一个是 Github Readme.md - https://github.com/gin-gonic/gin#serving-data-from-file

1
2
3
4
5
6
7
8
9
10
11
12
func main() {
router := gin.Default()

router.GET("/local/file", func(c *gin.Context) {
c.File("local/file.go")
})

var fs http.FileSystem = // ...
router.GET("/fs/file", func(c *gin.Context) {
c.FileFromFS("fs/file.go", fs)
})
}