表单,post的数据和上传的文件可以使用下面的 Context 的方法获取。

  1. // FormValueDefault 返回一个根据名字获取的form的值,
  2. // 其中可能是URL的查询参数和POST或者PUT的数据,
  3. // 如果没有找到返回 def 指定的值。
  4. FormValueDefault(name string, def string) string
  5. // FormValue 返回一个根据名字获取的form的值,
  6. // 其中可能是URL的查询参数和POST或者PUT的数据,
  7. FormValue(name string) string
  8. // FormValues 返回一个根据名字获取的form的值,
  9. // 其中可能是URL的查询参数和POST或者PUT的数据,
  10. // 默认的form的内存最大尺寸是32MB,
  11. // 这个可以通过在 app.Run() 的第二个参数传入 iris.WithPostMaxMemory 配置器来改变这个大小。
  12. // 记住:检查返回值是否为 nil 是有必要的!
  13. FormValues() map[string][]string
  14. // PostValueDefault 返回通过解析POST,PATCH或者PUT请求体参数,指定名字对应的值。
  15. // 如果没有找到这个名字则返回 def 指定的默认值。
  16. PostValueDefault(name string, def string) string
  17. // PostValue 返回通过解析POST,PATCH或者PUT请求体参数,指定名字对应的值。
  18. PostValue(name string) string
  19. // PostValueTrim 返回通过解析POST,PATCH或者PUT请求体参数,
  20. // 指定名字对应的没有前后空格的值。
  21. PostValueTrim(name string) string
  22. // PostValueInt 返回通过解析POST,PATCH或者PUT请求体参数,
  23. // 指定名字对应的int的值。
  24. // 如果没有找到name对应的值,则返回-1和一个非nil的错误。
  25. PostValueInt(name string) (int, error)
  26. // PostValueIntDefault 返回通过解析POST,PATCH或者PUT请求体参数,
  27. // 指定名字对应的int的值。
  28. // 如果没有找到name对应的值,则 def 指定的默认值。
  29. PostValueIntDefault(name string, def int) int
  30. // PostValueInt64 返回通过解析POST,PATCH或者PUT请求体参数,
  31. // 指定名字对应的int64的值。
  32. // 如果没有找到name对应的值,则返回-1和一个非nil的错误。
  33. PostValueInt64(name string) (int64, error)
  34. // PostValueInt64Default 返回通过解析POST,PATCH或者PUT请求体参数,
  35. // 指定名字对应的int64的值。
  36. // 如果没有找到name对应的值,则 def 指定的默认值。
  37. PostValueInt64Default(name string, def int64) int64
  38. // PostValueFloat64 返回通过解析POST,PATCH或者PUT请求体参数,
  39. // 指定名字对应的float6464的值。
  40. // 如果没有找到name对应的值,则返回-1和一个非nil的错误。
  41. PostValueFloat64(name string) (float64, error)
  42. // PostValueFloat64Default 返回通过解析POST,PATCH或者PUT请求体参数,
  43. // 指定名字对应的float64的值。
  44. // 如果没有找到name对应的值,则 def 指定的默认值。
  45. PostValueFloat64Default(name string, def float64) float64
  46. // PostValueBool 返回通过解析POST,PATCH或者PUT请求体参数,
  47. // 指定名字对应的bool的值。
  48. // 如果没有找到name对应的值,则返回false和一个非nil的错误。
  49. PostValueBool(name string) (bool, error)
  50. // PostValues 返回通过解析POST,PATCH或者PUT请求体参数,
  51. // 指定名字对应的[]string的值。
  52. // 默认的form的内存最大尺寸是32MB,
  53. // 这个可以通过在 app.Run() 的第二个参数传入 iris.WithPostMaxMemory 配置器来改变这个大小。
  54. // 记住:检查返回值是否为 nil 是有必要的!
  55. PostValues(name string) []string
  56. // FormFile 返回第一个从客户端上传的文件。
  57. // 默认的form的内存最大尺寸是32MB,
  58. // 这个可以通过在 app.Run() 的第二个参数传入 iris.WithPostMaxMemory 配置器来改变这个大小。
  59. // 记住:检查返回值是否为 nil 是有必要的!
  60. FormFile(key string) (multipart.File, *multipart.FileHeader, error)

Multipart/Urlencoded Form

  1. func main() {
  2. app := iris.Default()
  3. app.Post("/form_post", func(ctx iris.Context) {
  4. message := ctx.FormValue("message")
  5. nick := ctx.FormValueDefault("nick", "anonymous")
  6. ctx.JSON(iris.Map{
  7. "status": "posted",
  8. "message": message,
  9. "nick": nick,
  10. })
  11. })
  12. app.Run(iris.Addr(":8080"))
  13. }

另一个例子:query + post form

  1. POST /post?id=1234&page=1 HTTP/1.1
  2. Content-Type: application/x-www-form-urlencoded
  3. name=manu&message=this_is_great
  1. func main() {
  2. app := iris.Default()
  3. app.Post("/post", func(ctx iris.Context) {
  4. id := ctx.URLParam("id")
  5. page := ctx.URLParamDefault("page", "0")
  6. name := ctx.FormValue("name")
  7. message := ctx.FormValue("message")
  8. // or `ctx.PostValue` for POST, PUT & PATCH-only HTTP Methods.
  9. app.Logger().Infof("id: %s; page: %s; name: %s; message: %s",
  10. id, page, name, message)
  11. })
  12. app.Run(iris.Addr(":8080"))
  13. }
  1. id: 1234; page: 1; name: manu; message: this_is_great

上传文件

Context 提供了上传一个用于上传文件的助手(从请求的文件数据中保存文件到主机系统的硬盘上)。阅读下面的 Context.UploadFormFiles 方法。

  1. UploadFormFiles(destDirectory string,
  2. before ...func(Context, *multipart.FileHeader)) (n int64, err error)

UploadFromFile 上载任何从客户端获取的文件到系统物理 destDirctory 位置。

第二个参数 before 给定可调用的函数,这些函数可以在保存到磁盘之前改变 *miltipart.FileHeader,它可以用来基于当前请求改变文件的名字,并且所有的 FileHeader 的选项都可以改变。如果你不需要在保存文件到硬盘之前使用这个特性,你可以忽略这个参数。

请注意,它不会检查请求正文是否流式传输。

返回复制的长度的int64值, 和由于操作系统权限导致一个新文件无法创建的非 nil的错误, 或者由于没有文件获取而返回 net/http.ErrMissingFile 错误。

如果你想接收并接受文件,并且手动管理它们,你可以使用 Context.FormFile,创建一个复制的函数,满足你的需求,下面是通用用法。

默认的form的内存限制是32MB,你通过在主配置时传递 iris.WithPostMaxMemory 配置器到 app.Run 的第二个参数来改变这个限制。

示例代码:

  1. func main() {
  2. app := iris.Default()
  3. app.Post("/upload", iris.LimitRequestBodySize(maxSize), func(ctx iris.Context) {
  4. //
  5. // UploadFormFiles
  6. // uploads any number of incoming files ("multiple" property on the form input).
  7. //
  8. // The second, optional, argument
  9. // can be used to change a file's name based on the request,
  10. // at this example we will showcase how to use it
  11. // by prefixing the uploaded file with the current user's ip.
  12. ctx.UploadFormFiles("./uploads", beforeSave)
  13. })
  14. app.Run(iris.Addr(":8080"))
  15. }
  16. func beforeSave(ctx iris.Context, file *multipart.FileHeader) {
  17. ip := ctx.RemoteAddr()
  18. // make sure you format the ip in a way
  19. // that can be used for a file name (simple case):
  20. ip = strings.Replace(ip, ".", "_", -1)
  21. ip = strings.Replace(ip, ":", "_", -1)
  22. // you can use the time.Now, to prefix or suffix the files
  23. // based on the current time as well, as an exercise.
  24. // i.e unixTime := time.Now().Unix()
  25. // prefix the Filename with the $IP-
  26. // no need for more actions, internal uploader will use this
  27. // name to save the file into the "./uploads" folder.
  28. file.Filename = ip + "-" + file.Filename
  29. }