单个文件

引用问题 #774 和详细 示例代码

file.Filename 不应被信任。请参阅 MDN 上的 Content-Disposition#1693

文件名始终是可选的,应用程序不能盲目使用:应剥离路径信息,并应转换到服务器文件系统规则。

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) {
		// single file
		file, _ := c.FormFile("file")
		log.Println(file.Filename)

		// Upload the file to specific dst.
		c.SaveUploadedFile(file, dst)

		c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
	})
	router.Run(":8080")
}

如何 curl

curl -X POST http://localhost:8080/upload \
  -F "file=@/Users/appleboy/test.zip" \
  -H "Content-Type: multipart/form-data"
上次修改时间 2024 年 5 月 10 日:提升 GitHub 操作工作流 (#276) (4371021)