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.7k Javascript : Access JSON data example
+19.3k Golang : Check if directory exist and create if does not exist
+42k Golang : How do I convert int to uint8?
+51.4k Golang : Check if item is in slice/array
+5.3k Golang : The Tao of importing package
+12.6k Golang : Exit, terminating or aborting a program
+7.8k Golang : Scan files for certain pattern and rename part of the files
+6.6k Golang : How to validate ISBN?
+13.2k Golang : Convert(cast) int to int64
+41.3k Golang : How to count duplicate items in slice/array?
+5.3k Javascript : Change page title to get viewer attention
+13.7k Golang : Activate web camera and broadcast out base64 encoded images