多思考了一下,我选择方法 2 在使用 form file 后手动释放临时文件,本身是一个低频率功能,在 HandlerFunc 里面同时完成 Parse 和 RemoveAll ;而不是在 WithContext 时添加额外操作。
可能在 finshReq 执行 RemoveAll 就不是一个好的方法。
```go
func main() {
router := gin.Default()
router.Use(func(c *gin.Context) {
rr := c.Request
c.Request = c.Request.WithContext(c.Request.Context())
c.Next()
fmt.Println(rr.MultipartForm)
fmt.Println(c.Request.MultipartForm)
// fix 1
if rr.MultipartForm != c.Request.MultipartForm {
rr.MultipartForm = c.Request.MultipartForm
}
})
router.POST("/upload", func(c *gin.Context) {
c.Request.ParseMultipartForm(1<<10)
})
router.Run()
}
func main() {
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Request = c.Request.WithContext(c.Request.Context())
})
router.POST("/upload", func(c *gin.Context) {
c.Request.ParseMultipartForm(1<<10)
// fix 2
c.Requset.MultipartForm.RemoveAll()
c.Requset.MultipartForm.File = nil
})
router.Run()
}
```