update example for uploading files feature for ghttp.Server

This commit is contained in:
John 2019-11-06 10:39:59 +08:00
parent 2609db1aec
commit 6308380541
6 changed files with 100 additions and 25 deletions

View File

@ -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 {

View File

@ -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()
}
}

View File

@ -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(`
<html>
<head>
<title>GF UploadFile Demo</title>
<title>GF Upload File Demo</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
@ -50,11 +50,30 @@ func UploadShow(r *ghttp.Request) {
`)
}
// UploadShowBatch shows uploading multiple files page.
func UploadShowBatch(r *ghttp.Request) {
r.Response.Write(`
<html>
<head>
<title>GF Upload Files Demo</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="upload-file" />
<input type="file" name="upload-file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
`)
}
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()

View File

@ -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 {

View File

@ -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(`
<html>
<head>
<title>GF UploadFile Demo</title>
<title>GF Upload File Demo</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
@ -45,11 +50,30 @@ func UploadShow(r *ghttp.Request) {
`)
}
// UploadShowBatch shows uploading multiple files page.
func UploadShowBatch(r *ghttp.Request) {
r.Response.Write(`
<html>
<head>
<title>GF Upload Files Demo</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="upload-file" />
<input type="file" name="upload-file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
`)
}
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()

View File

@ -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
}
// 判断是否为静态文件请求