多文件

查看详细示例代码.

  1. func main() {
  2. router := gin.Default()
  3. // 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)
  4. router.MaxMultipartMemory = 8 << 20 // 8 MiB
  5. router.POST("/upload", func(c *gin.Context) {
  6. // Multipart form
  7. form, _ := c.MultipartForm()
  8. files := form.File["upload[]"]
  9. for _, file := range files {
  10. log.Println(file.Filename)
  11. // 上传文件至指定目录
  12. c.SaveUploadedFile(file, dst)
  13. }
  14. c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
  15. })
  16. router.Run(":8080")
  17. }

如何使用 curl:

  1. curl -X POST http://localhost:8080/upload \
  2. -F "upload[]=@/Users/appleboy/test1.zip" \
  3. -F "upload[]=@/Users/appleboy/test2.zip" \
  4. -H "Content-Type: multipart/form-data"

Last modified 27.05.2020 : chore: uncomment the code (#123) (7e8c3a0)