Created
September 21, 2017 17:37
-
-
Save suhanlee/67cecb3c1faec784b02c9e8e82760271 to your computer and use it in GitHub Desktop.
file_upload.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"io/ioutil" | |
"fmt" | |
) | |
func process_multipart_form(w http.ResponseWriter, r *http.Request) { | |
r.ParseMultipartForm(1024) | |
fileHeader := r.MultipartForm.File["uploaded"][0] | |
file, err := fileHeader.Open() | |
if err == nil { | |
data, err := ioutil.ReadAll(file) | |
if err == nil { | |
fmt.Fprintln(w, string(data)) | |
} | |
} | |
} | |
func process(w http.ResponseWriter, r *http.Request) { | |
file, _, err := r.FormFile("uploaded") | |
formHello := r.PostFormValue("hello") | |
formPost := r.PostFormValue("post") | |
queryHello := r.FormValue("hello") | |
if err == nil { | |
data, err := ioutil.ReadAll(file) | |
if err == nil { | |
fmt.Fprintln(w, string(data)) | |
} | |
} | |
fmt.Fprintln(w, formHello) | |
fmt.Fprintln(w, queryHello) | |
fmt.Fprintln(w, formPost) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/process", process) | |
server.ListenAndServe() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<form action="http://localhost:8080/process?hello=world&thread=123" method="post" enctype="multipart/form-data"> | |
<input type="text" name="hello" value="suhan" /> | |
<input type="text" name="post" value="456" /> | |
<input type="file" name="uploaded"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment