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
+3.7k MariaDB/MySQL : How to get version information
+4.1k Golang : micron to centimeter example
+26.5k Get file path of temporary file in Go
+7.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+8.9k Golang : Edge detection with Sobel method
+4.6k Unix/Linux/MacOSx : Get local IP address
+17.7k Golang : Delete duplicate items from a slice/array
+10.5k Golang : Sort and reverse sort a slice of runes
+9.5k Golang : Roll the dice example
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+18.2k Golang : Measure http.Get() execution time