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
+6.6k Golang : Reverse by word
+20.4k Golang : Pipe output from one os.Exec(shell command) to another command
+8.2k Golang : Count leading or ending zeros(any item of interest) example
+8.7k Golang : Executing and evaluating nested loop in html template
+9.1k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+79.5k Golang : How to return HTTP status code?
+7.2k Golang : File system scanning
+7.4k Golang : Rename part of filename
+5.6k Javascript : How to refresh page with JQuery ?
+13.8k Golang : unknown escape sequence error
+14.1k Golang : syscall.Socket example
+5.5k Python : Print unicode escape characters and string