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
+10.6k Golang : Generate Code128 barcode
+3.9k Linux/Unix/PHP : Restart PHP-FPM
+10k Golang : How to parse plain email text and process email header?
+19.4k Golang : How to reverse slice or array elements order
+8.3k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+16.2k Golang : Example for RSA package functions
+11.3k Facebook PHP getUser() returns 0
+13.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+26.7k Golang : Convert an image file to []byte
+24k Golang : Encrypt and decrypt data with AES crypto
+4.3k Golang : List all packages and search for certain package