Golang net/http.Request.FormFile() and FormValue() functions example
package net/http
Golang net/http.Request.FormFile() and FormValue() functions usage example
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file") // <-------------------------------- here!
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
out, err := os.Create("/tmp/uploadedfile")
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
return
}
defer out.Close()
// write the content from POST to the file
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w, "File uploaded successfully : ")
fmt.Fprintf(w, header.Filename)
}
and
func process_form_data(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
form_data := r.FormValue("form_data")
}
}
References :
https://www.socketloop.com/tutorials/get-form-post-value-go
http://golang.org/pkg/net/http/#Request.FormFile
Advertisement
Something interesting
Tutorials
+8.8k Golang : Executing and evaluating nested loop in html template
+5.4k Golang *File points to a file or directory ?
+14.5k Golang : Rename directory
+5.9k Golang : Detect variable or constant type
+20.2k Golang : Convert seconds to human readable time format example
+7.5k Golang : Rename part of filename
+11.7k Golang : Find age or leap age from date of birth example
+12k Golang : Convert a rune to unicode style string \u
+7.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.7k Unix/Linux/MacOSx : Get local IP address
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+22.9k Golang : Gorilla mux routing example