FormFile retrieves one uploaded file from a multipart HTTP request. It returns a file object, a metadata header containing information such as the filename, and an error when retrieval fails. section 10.2.3
Use FormFile when processing a form with one file per input field:
- Accept a POST form encoded as multipart/form-data. section 10.2.3
- Call r.FormFile("file") to obtain f, h, and err. section 10.2.3
- Use h.Filename when constructing the destination name, while treating the upload as binary data surrounded by metadata. section 10.2.3
Saving one uploaded file
func fileForm(w http.ResponseWriter, r *http.Request) { f, h, err := r.FormFile("file") if err != nil { panic(err) } defer f.Close() out, err := os.Create("/tmp/" + h.Filename) if err != nil { panic(err) } defer out.Close() io.Copy(out, f) fmt.Fprint(w, "Upload complete") }
