From 6308380541ffd3986051160238d9984ac4e8a7ae Mon Sep 17 00:00:00 2001 From: John Date: Wed, 6 Nov 2019 10:39:59 +0800 Subject: [PATCH] update example for uploading files feature for ghttp.Server --- .../net/ghttp/client/upload-batch/client.go | 9 ++- .../net/ghttp/client/upload-batch/client2.go | 22 +++++++ .../net/ghttp/client/upload-batch/server.go | 23 ++++++- .example/net/ghttp/client/upload/client.go | 2 +- .example/net/ghttp/client/upload/server.go | 60 +++++++++++++------ net/ghttp/ghttp_request.go | 9 ++- 6 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 .example/net/ghttp/client/upload-batch/client2.go diff --git a/.example/net/ghttp/client/upload-batch/client.go b/.example/net/ghttp/client/upload-batch/client.go index 492c5e421..a0f6629f8 100644 --- a/.example/net/ghttp/client/upload-batch/client.go +++ b/.example/net/ghttp/client/upload-batch/client.go @@ -2,14 +2,17 @@ package main import ( "fmt" - "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/glog" ) func main() { - path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go" - r, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18&upload-file=@file:"+path) + path1 := "/Users/john/Pictures/logo1.png" + path2 := "/Users/john/Pictures/logo2.png" + r, e := ghttp.Post( + "http://127.0.0.1:8199/upload", + fmt.Sprintf(`upload-file=@file:%s&upload-file=@file:%s`, path1, path2), + ) if e != nil { glog.Error(e) } else { diff --git a/.example/net/ghttp/client/upload-batch/client2.go b/.example/net/ghttp/client/upload-batch/client2.go new file mode 100644 index 000000000..24568e951 --- /dev/null +++ b/.example/net/ghttp/client/upload-batch/client2.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/glog" +) + +func main() { + path1 := "/Users/john/Pictures/logo1.png" + path2 := "/Users/john/Pictures/logo2.png" + r, e := ghttp.Post( + "http://127.0.0.1:8199/upload", + fmt.Sprintf(`upload-file[]=@file:%s&upload-file[]=@file:%s`, path1, path2), + ) + if e != nil { + glog.Error(e) + } else { + fmt.Println(string(r.ReadAll())) + r.Close() + } +} diff --git a/.example/net/ghttp/client/upload-batch/server.go b/.example/net/ghttp/client/upload-batch/server.go index 8e9e03776..d3a83eeb0 100644 --- a/.example/net/ghttp/client/upload-batch/server.go +++ b/.example/net/ghttp/client/upload-batch/server.go @@ -33,12 +33,12 @@ func Upload(r *ghttp.Request) { r.Response.Write("upload successfully") } -// UploadShow shows uploading page. +// UploadShow shows uploading simgle file page. func UploadShow(r *ghttp.Request) { r.Response.Write(` - GF UploadFile Demo + GF Upload File Demo
@@ -50,11 +50,30 @@ func UploadShow(r *ghttp.Request) { `) } +// UploadShowBatch shows uploading multiple files page. +func UploadShowBatch(r *ghttp.Request) { + r.Response.Write(` + + + GF Upload Files Demo + + + + + + +
+ + + `) +} + func main() { s := g.Server() s.Group("/upload", func(g *ghttp.RouterGroup) { g.ALL("/", Upload) g.ALL("/show", UploadShow) + g.ALL("/batch", UploadShowBatch) }) s.SetPort(8199) s.Run() diff --git a/.example/net/ghttp/client/upload/client.go b/.example/net/ghttp/client/upload/client.go index 492c5e421..0199e3441 100644 --- a/.example/net/ghttp/client/upload/client.go +++ b/.example/net/ghttp/client/upload/client.go @@ -9,7 +9,7 @@ import ( func main() { path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go" - r, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18&upload-file=@file:"+path) + r, e := ghttp.Post("http://127.0.0.1:8199/upload", "upload-file=@file:"+path) if e != nil { glog.Error(e) } else { diff --git a/.example/net/ghttp/client/upload/server.go b/.example/net/ghttp/client/upload/server.go index 27f4322ee..d3a83eeb0 100644 --- a/.example/net/ghttp/client/upload/server.go +++ b/.example/net/ghttp/client/upload/server.go @@ -7,33 +7,38 @@ import ( "io" ) -// Upload uploads file to /tmp . +// Upload uploads files to /tmp . func Upload(r *ghttp.Request) { - f, h, e := r.FormFile("upload-file") - if e != nil { - r.Response.Write(e) - } - defer f.Close() - savePath := "/tmp/" + gfile.Basename(h.Filename) - file, err := gfile.Create(savePath) - if err != nil { - r.Response.Write(err) - return - } - defer file.Close() - if _, err := io.Copy(file, f); err != nil { - r.Response.Write(err) - return + saveDir := "/tmp/" + for _, item := range r.GetMultiPartFiles("upload-file") { + file, err := item.Open() + if err != nil { + r.Response.Write(err) + return + } + defer file.Close() + + f, err := gfile.Create(saveDir + gfile.Basename(item.Filename)) + if err != nil { + r.Response.Write(err) + return + } + defer f.Close() + + if _, err := io.Copy(f, file); err != nil { + r.Response.Write(err) + return + } } r.Response.Write("upload successfully") } -// UploadShow shows uploading page. +// UploadShow shows uploading simgle file page. func UploadShow(r *ghttp.Request) { r.Response.Write(` - GF UploadFile Demo + GF Upload File Demo
@@ -45,11 +50,30 @@ func UploadShow(r *ghttp.Request) { `) } +// UploadShowBatch shows uploading multiple files page. +func UploadShowBatch(r *ghttp.Request) { + r.Response.Write(` + + + GF Upload Files Demo + + + + + + +
+ + + `) +} + func main() { s := g.Server() s.Group("/upload", func(g *ghttp.RouterGroup) { g.ALL("/", Upload) g.ALL("/show", UploadShow) + g.ALL("/batch", UploadShowBatch) }) s.SetPort(8199) s.Run() diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index c983cc18c..f5b20cb54 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -219,7 +219,14 @@ func (r *Request) GetMultiPartFiles(name string) []*multipart.FileHeader { if r.MultipartForm == nil { return nil } - return r.MultipartForm.File[name] + if v := r.MultipartForm.File[name]; len(v) > 0 { + return v + } + // Support "name[]" as array parameter. + if v := r.MultipartForm.File[name+"[]"]; len(v) > 0 { + return v + } + return nil } // 判断是否为静态文件请求