multipart.FileHeader is a pointer-backed Go structure that represents one uploaded multipart file. It exposes uploaded metadata and provides Open, which returns a file handler for reading the contents. section 10.2.4
Use a FileHeader when processing one or more uploaded files:
- Call ParseMultipartForm before reading multipart fields; its memory limit controls how much form data stays in memory, with remaining files written to temporary disk storage. section 10.2.4
- Retrieve multiple uploads from Request.MultipartForm.File, where each entry is a *multipart.FileHeader. section 10.2.4
- Call Open to read a header's contents, use Filename for a local destination name, and copy the data with io.Copy. section 10.2.4
- Inspect Header["Content-Type"] when assessing the uploader-provided MIME type, while recognizing that the value may be generic or untrusted. section 10.2.5
Process several uploaded files
After parsing the request, iterate over r.MultipartForm.File["files"]. For each header, call fh.Open(), create a destination file, and copy the uploaded contents. The multiple-value input field is named files and uses multipart/form-data. section 10.2.4
file, header, err := r.FormFile("file")
contentType := header.Header["Content-Type"][0]
Inspect the uploaded Content-Type through the FileHeader Header field after calling Request.FormFile. section 10.2.5
file, header, err := r.FormFile("file")
extension := filepath.Ext(header.Filename)
type := mime.TypeByExtension(extension)
Derive a tentative MIME type from FileHeader.Filename with filepath.Ext and mime.TypeByExtension. section 10.2.5
file, header, err := r.FormFile("file")
buffer := make([]byte, 512)
_, err = file.Read(buffer)
filetype := http.DetectContentType(buffer)
Read file bytes returned by FormFile and use http.DetectContentType to infer type from contents. section 10.2.5
