Search code examples
gogo-fiber

With Fiber's context, how do I iterate over multiple files?


When I receive a post request with a list of files to be uploaded to the server, I can get a specific file, if I know the name of it via

c.FormFile("filename")

But how would I iterate over the files in that list, without knowing the files names ahead of time? I don't see a method listed in the context docs that simply provides a list of files.


Solution

  • Call c.MultiPartForm() to get a *multipart.Form. Iterate through the form's File field.

    form, err := ctx.MultipartForm()
    if err != nil { /* handle error */ }
    for formFieldName, fileHeaders := range form.File {
        for _, fileHeader := range fileHeaders {
            // process uploaded file here
        }
    }