From 0c71408d1f51088668ba261cf7287eb7241eca21 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 21 Nov 2018 17:03:31 +0800 Subject: [PATCH] add file close; add multipart close; --- multipart.go | 12 ++++++++++-- upload_file.go | 2 -- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/multipart.go b/multipart.go index 64162c7..cb1b3af 100644 --- a/multipart.go +++ b/multipart.go @@ -15,6 +15,11 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) { log.Panic(err) } io.Copy(part, ufile.FileReaderCloser) + + err = ufile.FileReaderCloser.Close() + if err != nil { + panic(err) + } } func createMultipart(postParams IBody, params []interface{}) { @@ -23,8 +28,6 @@ func createMultipart(postParams IBody, params []interface{}) { body := &bytes.Buffer{} mwriter := multipart.NewWriter(body) - defer mwriter.Close() - for _, iparam := range params[0 : plen-1] { switch param := iparam.(type) { case *UploadFile: @@ -87,4 +90,9 @@ func createMultipart(postParams IBody, params []interface{}) { postParams.AddContentType(mwriter.FormDataContentType()) postParams.SetIOBody(body) + + err := mwriter.Close() + if err != nil { + panic(err) + } } diff --git a/upload_file.go b/upload_file.go index e9d07dd..e034e27 100644 --- a/upload_file.go +++ b/upload_file.go @@ -17,13 +17,11 @@ type UploadFile struct { // UploadFileFromPath 从本地文件获取上传文件 func UploadFileFromPath(fileName string) (*UploadFile, error) { fd, err := os.Open(fileName) - if err != nil { return nil, err } return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil - } // UploadFileFromGlob 根据Glob从本地文件获取上传文件