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
+40.7k Golang : How to count duplicate items in slice/array?
+30.8k Golang : bufio.NewReader.ReadLine to read file line by line
+6.4k Golang : Check if password length meet the requirement
+12.3k Golang : Add ASCII art to command line application launching process
+7.1k Golang : Check to see if *File is a file or directory
+13.9k Elastic Search : Mapping date format and sort by date
+9.8k Golang : Test a slice of integers for odd and even numbers
+19.5k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+5.1k Unix/Linux : How to archive and compress entire directory ?
+27k Golang : dial tcp: too many colons in address
+7.3k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+16.5k Golang : read gzipped http response