mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 12:17:53 +08:00
update example for uploading files feature for ghttp.Server
This commit is contained in:
parent
2609db1aec
commit
6308380541
@ -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 {
|
||||
|
22
.example/net/ghttp/client/upload-batch/client2.go
Normal file
22
.example/net/ghttp/client/upload-batch/client2.go
Normal 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()
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
// 判断是否为静态文件请求
|
||||
|
Loading…
Reference in New Issue
Block a user