Golang net/http.Request.ParseMultipartForm() function example

package net/http

Golang net/http.Request.ParseMultipartForm() function usage example

 func uploadHandler(w http.ResponseWriter, r *http.Request) {

  err := r.ParseMultipartForm(200000) // grab the multipart form
  if err != nil {
 fmt.Fprintln(w, err)
 return
  }

  formdata := r.MultipartForm // ok, no problem so far, read the Form data

  //get the *fileheaders
  files := formdata.File["multiplefiles"] // grab the filenames

  for i, _ := range files { // loop through the files one by one
 file, err := files[i].Open()
 defer file.Close()
 if err != nil {
 fmt.Fprintln(w, err)
 return
 }
  }
  ...

see full example at : https://www.socketloop.com/tutorials/upload-multiple-files-golang

References :

http://golang.org/pkg/net/http/#Request.ParseMultipartForm

https://www.socketloop.com/tutorials/upload-multiple-files-golang

Advertisement