add file close; add multipart close;

This commit is contained in:
huangsimin 2018-11-21 17:03:31 +08:00
parent 9d6dee25c9
commit 0c71408d1f
2 changed files with 10 additions and 4 deletions

View File

@ -15,6 +15,11 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) {
log.Panic(err) log.Panic(err)
} }
io.Copy(part, ufile.FileReaderCloser) io.Copy(part, ufile.FileReaderCloser)
err = ufile.FileReaderCloser.Close()
if err != nil {
panic(err)
}
} }
func createMultipart(postParams IBody, params []interface{}) { func createMultipart(postParams IBody, params []interface{}) {
@ -23,8 +28,6 @@ func createMultipart(postParams IBody, params []interface{}) {
body := &bytes.Buffer{} body := &bytes.Buffer{}
mwriter := multipart.NewWriter(body) mwriter := multipart.NewWriter(body)
defer mwriter.Close()
for _, iparam := range params[0 : plen-1] { for _, iparam := range params[0 : plen-1] {
switch param := iparam.(type) { switch param := iparam.(type) {
case *UploadFile: case *UploadFile:
@ -87,4 +90,9 @@ func createMultipart(postParams IBody, params []interface{}) {
postParams.AddContentType(mwriter.FormDataContentType()) postParams.AddContentType(mwriter.FormDataContentType())
postParams.SetIOBody(body) postParams.SetIOBody(body)
err := mwriter.Close()
if err != nil {
panic(err)
}
} }

View File

@ -17,13 +17,11 @@ type UploadFile struct {
// UploadFileFromPath 从本地文件获取上传文件 // UploadFileFromPath 从本地文件获取上传文件
func UploadFileFromPath(fileName string) (*UploadFile, error) { func UploadFileFromPath(fileName string) (*UploadFile, error) {
fd, err := os.Open(fileName) fd, err := os.Open(fileName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil
} }
// UploadFileFromGlob 根据Glob从本地文件获取上传文件 // UploadFileFromGlob 根据Glob从本地文件获取上传文件