How to get file upload field name (finished)

If with go monitoring server POSTs how to save file upload if do not know field name?!
In this code line:
file, handler, err := r.FormFile(“uploadfile”)

if i don’t know field name:“uploadfile”, how can i get that file and save it?!

Is r a http.Request?


Finaly i get it working. First i detect if post is multipart with function:

func isMultipart(r *http.Request) bool {
  ct := r.Header.Get("Content-Type")
  ctm := strings.Split(ct, ";")

  if ctm[0] == "multipart/form-data" {
    return true

  return false

And main function to save uploaded files to TEST directory with not specified upload field name:

if isMultipart(r) {
  err := r.ParseMultipartForm(1 << 20)

  if err == nil {
    formData := r.MultipartForm
    keys := reflect.ValueOf(formData).Elem()
    // Second map in Field() is file fields
    // First map is all input fields exclude file upload fields
    uloadFileFieldMaps := keys.Field(1)
    if uloadFileFieldMaps.Kind() == reflect.Map {
      uploadFieldNames := uloadFileFieldMaps.MapKeys()
      for _, uploadFieldName := range uploadFieldNames {
        file, handler, err := r.FormFile(uploadFieldName.String())

        if err != nil {
        defer file.Close()
        f, err := os.OpenFile("./test/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
        defer f.Close()
        io.Copy(f, file)

Hope someone will be useful

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.