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
+15.4k Golang : Update database with GORM example
+6.3k Golang : Check if password length meet the requirement
+8k Golang : Count leading or ending zeros(any item of interest) example
+10.7k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+19.2k Golang : Set or Add HTTP Request Headers
+20.8k Golang : Convert(cast) string to rune and back to string example
+12.7k Golang : How to get a user home directory path?
+11.8k Golang : Display list of countries and ISO codes
+6.7k Golang : Squaring elements in array
+6.9k Golang : How to detect if a sentence ends with a punctuation?
+15.6k Golang : Get sub string example
+20.7k Golang : How to get time zone and load different time zone?