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
+22.5k Golang : Set and Get HTTP request headers example
+17.1k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+13.8k Golang : convert rune to unicode hexadecimal value and back to rune character
+5.1k Golang : Print instead of building pyramids
+10.4k Golang : Get local time and equivalent time in different time zone
+18.5k Golang : Iterating Elements Over A List
+5.9k Javascript : Get operating system and browser information
+6.2k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+46.2k Golang : Encode image to base64 example
+7.7k Golang : Get today's weekday name and calculate target day distance example
+12.3k Golang : "https://" not allowed in import path
+11.9k Golang : Detect user location with HTML5 geo-location