文件上传

GF支持非常方便的表单文件上传功能,并且HTTP客户端对上传功能进行了必要的封装并极大简化了上传功能调用。

服务端

https://github.com/gogf/gf/blob/master/.example/net/ghttp/client/upload/server.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. "github.com/gogf/gf/net/ghttp"
  5. )
  6. // Upload uploads files to /tmp .
  7. func Upload(r *ghttp.Request) {
  8. files := r.GetUploadFiles("upload-file")
  9. names, err := files.Save("/tmp/")
  10. if err != nil {
  11. r.Response.WriteExit(err)
  12. }
  13. r.Response.WriteExit("upload successfully: ", names)
  14. }
  15. // UploadShow shows uploading simgle file page.
  16. func UploadShow(r *ghttp.Request) {
  17. r.Response.Write(`
  18. <html>
  19. <head>
  20. <title>GF Upload File Demo</title>
  21. </head>
  22. <body>
  23. <form enctype="multipart/form-data" action="/upload" method="post">
  24. <input type="file" name="upload-file" />
  25. <input type="submit" value="upload" />
  26. </form>
  27. </body>
  28. </html>
  29. `)
  30. }
  31. // UploadShowBatch shows uploading multiple files page.
  32. func UploadShowBatch(r *ghttp.Request) {
  33. r.Response.Write(`
  34. <html>
  35. <head>
  36. <title>GF Upload Files Demo</title>
  37. </head>
  38. <body>
  39. <form enctype="multipart/form-data" action="/upload" method="post">
  40. <input type="file" name="upload-file" />
  41. <input type="file" name="upload-file" />
  42. <input type="submit" value="upload" />
  43. </form>
  44. </body>
  45. </html>
  46. `)
  47. }
  48. func main() {
  49. s := g.Server()
  50. s.Group("/upload", func(group *ghttp.RouterGroup) {
  51. group.POST("/", Upload)
  52. group.ALL("/show", UploadShow)
  53. group.ALL("/batch", UploadShowBatch)
  54. })
  55. s.SetPort(8199)
  56. s.Run()
  57. }

该服务端提供了3个接口:

  1. http://127.0.0.1:8199/upload/show 地址用于展示单个文件上传的H5页面;
  2. http://127.0.0.1:8199/upload/batch 地址用于展示多个文件上传的H5页面;
  3. http://127.0.0.1:8199/upload 接口用于真实的表单文件上传,该接口同时支持单个文件或者多个文件上传;

我们这里访问 http://127.0.0.1:8199/upload/show 选择需要上传的单个文件,提交之后可以看到文件上传成功到服务器上。

关键代码说明

  1. 我们在服务端可以通过r.GetUploadFiles方法获得上传的所有文件对象,也可以通过r.GetUploadFile获取单个上传的文件对象;此外,r.GetUploadFiles方法的第二个参数支持随机自动命名上传文件。
  2. r.GetUploadFiles("upload-file")中的参数"upload-file"为本示例中客户端上传时的表单文件域名称,开发者可以根据前后端约定在客户端中定义,以方便服务端接收表单文件域参数。
  3. 通过files.Save可以将上传的多个文件方便地保存到指定的目录下,并返回保存成功的文件名。如果是批量保存,只要任意一个文件保存失败,都将会立即返回错误。
  4. 通过group.POST("/", Upload)注册的路由仅支持POST方式访问。

客户端

单文件上传

https://github.com/gogf/gf/blob/master/.example/net/ghttp/client/upload/client.go

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/net/ghttp"
  5. "github.com/gogf/gf/os/glog"
  6. )
  7. func main() {
  8. path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go"
  9. r, e := ghttp.Post("http://127.0.0.1:8199/upload", "upload-file=@file:"+path)
  10. if e != nil {
  11. glog.Error(e)
  12. } else {
  13. fmt.Println(string(r.ReadAll()))
  14. r.Close()
  15. }
  16. }

注意到了吗?文件上传参数格式使用了 参数名=@file:文件路径 ,HTTP客户端将会自动解析文件路径对应的文件内容并读取提交给服务端。原本复杂的文件上传操作被gf进行了封装处理,用户只需要使用 @file:+文件路径 来构成参数值即可。其中,文件路径请使用本地文件绝对路径。

首先运行服务端程序之后,我们再运行这个上传客户端(注意修改上传的文件路径为本地真实文件路径),执行后可以看到文件被成功上传到服务器的指定路径下。

多文件上传

https://github.com/gogf/gf/blob/master/.example/net/ghttp/client/upload-batch/client.go

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/net/ghttp"
  5. "github.com/gogf/gf/os/glog"
  6. )
  7. func main() {
  8. path1 := "/Users/john/Pictures/logo1.png"
  9. path2 := "/Users/john/Pictures/logo2.png"
  10. r, e := ghttp.Post(
  11. "http://127.0.0.1:8199/upload",
  12. fmt.Sprintf(`upload-file=@file:%s&upload-file=@file:%s`, path1, path2),
  13. )
  14. if e != nil {
  15. glog.Error(e)
  16. } else {
  17. fmt.Println(string(r.ReadAll()))
  18. r.Close()
  19. }
  20. }

可以看到,多个文件上传提交参数格式为参数名=@file:xxx&参数名=@file:xxx...,也可以使用参数名[]=@file:xxx&参数名[]=@file:xxx...的形式。

首先运行服务端程序之后,我们再运行这个上传客户端(注意修改上传的文件路径为本地真实文件路径),执行后可以看到文件被成功上传到服务器的指定路径下。