Home > OS >  How to receive uploaded files as array
How to receive uploaded files as array

Time:10-14

How to receive multipart form where the files are sent as an array <input type="file" name="files[]"> ?

This returns an empty result

fhs := r.MultipartForm.File["files"]
fmt.Printf("Files: %v", fhs)

Is it possible to list all input/field keys?

CodePudding user response:

Call ParseMultipartForm before using the request's MultipartForm field.

Get the slice of files for the form field named files[] using this code:

fhs := r.MultipartForm.File["files[]"]

List all values and files by ranging over the maps and slices:

for k, vs := range r.MultipartForm.Value {
    for _, v := range vs {
        fmt.Println("value %s: %s\n", k, v)
    }
}
for k, fs := range r.MultipartForm.File {
    for _, f := range fs {
        fmt.Println("file %s: %s\n", k, f.Filename)
    }
}
  •  Tags:  
  • go
  • Related