2

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 Answer 1

3

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 << 20); err != nil {
    // handle error
}

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

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your response. Trouble is, I'm trying to get a set of data for each array item if that makes sense. So I'm not just iterating over a series of files, each iteration will contain a height and width value also.
Are you trying to derive the height and width from the uploaded file, or is it inputed when the file is uploaded?
If you are trying to derive the width and height you will have to open the file and decode the image based on mimetype to determine the width and height.
It's just a generic upload, I'm uploading several files, each given a height and width and I have a function which crops that image to the height and width etc. I'm just struggling to access the post data in rows if that makes sense?
Ok. You can just use a regular form array for width and height that correspond to the order that the files were uploaded. Then when you iterate your files you can accesses the form's Value map to get the width and height by index.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.