在Golang(gin)中访问数组的POST参数。

huangapple go评论153阅读模式
英文:

Accessing array post params in golang (gin)

问题

我正在尝试访问一个由Gin(golang)编写的API中的文件和值数组。我有一个函数,它接受一个文件、高度和宽度。然后它调用函数来调整文件的大小,然后将其上传到S3。然而,我还尝试上传多个文件。

func (rc *ResizeController) Resize(c *gin.Context) {

    file, header, err := c.Request.FormFile("file")
    filename := header.Filename

    if err != nil {
        log.Fatal(err)
    }

    height := c.PostForm("height")
    width := c.PostForm("width")

    finalFile := rc.Crop(height, width, file)

    go rc.Upload(filename, finalFile, "image/jpeg", s3.BucketOwnerFull)

    c.JSON(200, gin.H{"filename": filename})
}

我在文档中没有找到如何访问以下格式数据的任何地方:

item[0]file
item[0]width
item[0]height
item[1]file
item[1]width
item[1]height

等等。

我想到了以下类似的解决方案:

for index, element := range c.Request.PostForm("item") {
    fmt.Println(element.Height)
}

但是报错了:"c.Request.Values undefined (type *http.Request has no field or method Values)"

英文:

I'm attempting to access an array of files and values posted to an API written in Gin (golang). I've got a function which takes a file, height and width. It then calls functions to resize the file, and then upload it to S3. However, I'm attempting to also upload multiple files.

func (rc *ResizeController) Resize(c *gin.Context) {

    file, header, err := c.Request.FormFile("file")
    filename := header.Filename

    if err != nil {
	    log.Fatal(err)
    }

    height := c.PostForm("height")
    width := c.PostForm("width")

    finalFile := rc.Crop(height, width, file)

    go rc.Upload(filename, finalFile, "image/jpeg", s3.BucketOwnerFull)

    c.JSON(200, gin.H{"filename": filename})
}

I couldn't see anywhere in the docs how to access data in the following format:

item[0]file
item[0]width
item[0]height
item[1]file
item[1]width
item[1]height

etc.

I figured something along the lines of:

for index, element := range c.Request.PostForm("item") {
    fmt.Println(element.Height)
}

But that threw "c.Request.Values undefined (type *http.Request has no field or method Values)"

答案1

得分: 3

你可以直接访问File切片,而不是使用Request上的FormFile方法。假设你有一个与上传文件的顺序对应的widthheight的表单数组。

if err := ctx.Request.ParseMultipartForm(32 << 20); err != nil {
    // 处理错误
}

for i, fh := range ctx.Request.MultipartForm.File["item"] {
    // 使用fh访问文件头
    w := ctx.Request.MultipartForm.Value["width"][i]
    h := ctx.Request.MultipartForm.Value["height"][i]
}

Request上的FormFile方法只是MultipartForm.File的一个包装,它返回该键的第一个文件。

英文:

You can access the File slice directly instead of using the FormFile method on Request. Assuming you have a form array for width and height that correspond to the order that the files were uploaded.

if err := ctx.Request.ParseMultipartForm(32 &lt;&lt; 20); err != nil {
    // handle error
}

for i, fh := range ctx.Request.MultipartForm.File[&quot;item&quot;] {
    // access file header using fh
    w := ctx.Request.MultipartForm.Value[&quot;width&quot;][i]
    h := ctx.Request.MultipartForm.Value[&quot;height&quot;][i]
}

The FormFile method on Request is just a wrapper around MultipartForm.File that returns the first file at that key.

huangapple
  • 本文由 发表于 2015年10月16日 23:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/33174247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定