From 04e75f471290907655a2e1c97005d9553241995b Mon Sep 17 00:00:00 2001 From: cncal Date: Sat, 11 Mar 2023 15:51:52 +0800 Subject: [PATCH] improve the context.MultipartForm method --- context.go | 8 ++++++-- context_test.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 5716318e..45ea4b90 100644 --- a/context.go +++ b/context.go @@ -591,8 +591,12 @@ func (c *Context) FormFile(name string) (*multipart.FileHeader, error) { // MultipartForm is the parsed multipart form, including file uploads. func (c *Context) MultipartForm() (*multipart.Form, error) { - err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory) - return c.Request.MultipartForm, err + if c.Request.MultipartForm == nil { + if err := c.Request.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { + return nil, err + } + } + return c.Request.MultipartForm, nil } // SaveUploadedFile uploads the form file to specific dst. diff --git a/context_test.go b/context_test.go index 1dec902c..58888552 100644 --- a/context_test.go +++ b/context_test.go @@ -107,7 +107,7 @@ func TestContextMultipartForm(t *testing.T) { if assert.NoError(t, err) { assert.NotNil(t, f) } - + assert.Equal(t, c.PostForm("foo"), "bar") assert.NoError(t, c.SaveUploadedFile(f.File["file"][0], "test")) }