From c143a6ec19accb1be464666e197049dddc276275 Mon Sep 17 00:00:00 2001 From: astaxie Date: Sat, 13 Jun 2015 16:20:26 +0800 Subject: [PATCH] fix #1090 add Getfiles to support mulit file upload --- controller.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/controller.go b/controller.go index 81ee1230..9011593a 100644 --- a/controller.go +++ b/controller.go @@ -499,6 +499,41 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, return c.Ctx.Request.FormFile(key) } +// GetFiles return multi-upload files +// files, err:=c.Getfiles("myfiles") +// if err != nil { +// http.Error(w, err.Error(), http.StatusNoContent) +// return +// } +// for i, _ := range files { +// //for each fileheader, get a handle to the actual file +// file, err := files[i].Open() +// defer file.Close() +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// //create destination file making sure the path is writeable. +// dst, err := os.Create("upload/" + files[i].Filename) +// defer dst.Close() +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// //copy the uploaded file to the destination file +// if _, err := io.Copy(dst, file); err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// } +func (c *Controller) GetFiles(key string) ([]*multipart.FileHeader, error) { + files, ok := c.Ctx.Request.MultipartForm.File["key"] + if ok { + return files, nil + } + return nil, http.ErrMissingFile +} + // SaveToFile saves uploaded file to new path. // it only operates the first one of mutil-upload form file field. func (c *Controller) SaveToFile(fromfile, tofile string) error {