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
+4.8k Golang : Process non-XML/JSON formatted ASCII text file example
+5k Golang : How to determine if request or crawl is from Google robots
+17.6k Golang : How to force compile or remove object files first before rebuild?
+9.9k Golang : Verify Linux user password again before executing a program example
+13k Golang : Find location by IP address and display with Google Map
+4k Unix/Linux/MacOSx : How to remove an environment variable ?
+4.1k Golang : Stop goroutine without channel
+16k Golang : Implement getters and setters
+7.1k Golang : How to extract video or image files from html source code
+5.2k CloudFlare : Another way to get visitor's real IP address
+4.8k Golang : Convert an executable file into []byte example
+3.9k Golang : The Tao of importing package