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.9k Golang : Find the length of big.Int variable example
+18.3k Golang : Check if os.Stdin input data is piped or from terminal
+7.8k Golang : Build and compile multiple source files
+6.4k Golang : Not able to grep log.Println() output
+7.3k Useful methods to access blocked websites
+13.5k Golang : Execute function at intervals or after some delay
+10.5k Golang : Find and draw contours with OpenCV example
+15.9k Golang : Check if IP address is version 4 or 6
+12.7k Golang : How to pass map to html template and access the map's elements
+12.9k Golang : concatenate(combine) strings
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)