I send the form to the server to which I attach the image files. Initially, the number of pictures is not known. The form has this form

<input type="file" name="file_1"> 

If the user has added several photos, then there will be

 <input type="file" name="file_1"> <input type="file" name="file_2"> 

I take data from one file so file, _, err := r.FormFile("file_1")
How to go find out the number of files and sort through each of them?

  • Most likely this is written in the library documentation. What library do you use to parse the request? - rekby

1 answer 1

 func uploadHandler(w http.ResponseWriter, r *http.Request) { //парсим мультипарт форму err := r.ParseMultipartForm(200000) if err != nil { fmt.Fprintln(w, err) return } //получаем формконтейнер с указателями на распарсенные формы //доступно только после вызова `ParseMultipartForm` formdata := r.MultipartForm //и получаем список указателей на файловые дескрипторы именованной формы со списком файлов //по сути это список открытых дескрипторов, так что обычный len(files) покажет общее количество файлов files := formdata.File["multiplefiles"] fmt.Printf("Total files: %v\n", files) } 
  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky