Golang : Get uploaded file name or access uploaded files
A regular community member of Golang Facebook group asked a question recently on how to capture the uploaded filename in a single method. Below is the quick way of getting the filename:
UPDATE:
You can use this method as well to access uploaded files information. Such as the MIME or metadata information.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("file") // the input file by form
defer file.Close()
if err != nil {
fmt.Fprintln(w, err)
return
}
filename := header.Filename // get the filename
fmt.Fprintf(w,"Filename is" + filename + "\n")
}
Reference :
See also : Golang : Upload file from web browser to server
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.2k Golang : Number guessing game with user input verification example
+10.4k Golang : Generate DSA private, public key and PEM files example
+12.1k Facebook PHP getUser() returns 0
+7.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+7.7k Golang : Gonum standard normal random numbers example
+35.1k Golang : Convert date or time stamp from string to time.Time type
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+4.8k nginx : force all pages to be SSL
+15.5k Golang : Gzip file example
+4.2k Python : Convert(cast) bytes to string example
+7.4k Golang : Convert word to its plural form example
+16.5k Golang : Aligning strings to right, left and center with fill example