Golang mime/multipart.FileHeader.Open() function example

package mime/multipart

Golang mime/multipart.FileHeader.Open() function usage example

 formdata := r.MultipartForm // ok, no problem so far, read the Form data
 //get the *fileheaders
 fileHeaders := formdata.File["multiplefiles"] // grab the filenames

 for i, _ := range fileHeaders { // loop through the files one by one
 file, err := fileHeaders[i].Open() // <--------------------- here!
 defer file.Close()
 if err != nil {
 fmt.Fprintln(w, err)
 return
  }
  w.Write([]byte(fmt.Sprintf("Filename : %s open successfully.", fileHeaders[i].Filename)))
 }

References :

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

http://golang.org/pkg/mime/multipart/#FileHeader.Open

Advertisement