GITHUB网址:
https://github.com/astaxie/build-web-application-with-golang
内容
- 4.表单
login.gtpl
<html> <head> <title> hello, go form </title> </head> <body> <form action="/login" method="post"> <input type="checkbox" name="interest" value="football">足球 <input type="checkbox" name="interest" value="basketball">篮球 <input type="checkbox" name="interest" value="tennis">网球 username:<input type="text" name="username"> age:<input type="text" name="age"> password:<input type="password" name="password"> <input type="hidden" name="token" value="{{.}}"> <input type="submit" value="login"> </form> </body> </html>
upload.gtpl
<html> <head> <title> hello, go upload </title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="uploadfile" /> <input type="hidden" name="token" value="{{.}}"> <input type="submit" value="upload"> </form> </body> </html>
go.main
package main import ( "crypto/md5" "fmt" "html/template" "io" "log" "net/http" "os" "strconv" "strings" "time" ) func sayHelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key: ", k) fmt.Println("val: ", strings.Join(v, "")) } fmt.Fprintf(w, "Hello astaxie!") } func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method: ", r.Method) if r.Method == "GET" { crutime := time.Now().Unix() h := md5.New() io.WriteString(h, strconv.FormatInt(crutime, 10)) token := fmt.Sprintf("%x", h.Sum(nil)) t, _ := template.ParseFiles("login.gtpl") t.Execute(w, token) log.Println(t.Execute(w, nil)) } else { r.ParseForm() token := r.Form.Get("token") if token != "" { log.Printf("token is ok") } else { log.Fatal("no token") } fmt.Println("username: ", template.HTMLEscapeString(r.Form.Get("username"))) fmt.Println("password:", r.Form["password"]) fmt.Println("token:", r.Form["token"]) age, err := strconv.Atoi(r.Form.Get("age")) if err != nil { log.Print(err) } if age > 100 { log.Print("too long") } else { fmt.Println("age:", r.Form["age"]) } template.HTMLEscape(w, []byte(r.Form.Get("username"))) } } func upload(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) if r.Method == "GET" { crutime := time.Now().Unix() h := md5.New() io.WriteString(h, strconv.FormatInt(crutime, 10)) token := fmt.Sprintf("%x", h.Sum(nil)) t, _ := template.ParseFiles("upload.gtpl") t.Execute(w, token) } else { r.ParseMultipartForm(32 << 20) file, handler, err := r.FormFile("uploadfile") if err != nil { fmt.Println(err) return } defer file.Close() fmt.Fprintf(w, "%v", handler.Header) f, err := os.OpenFile("./"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) return } defer f.Close() io.Copy(f, file) } } func main() { http.HandleFunc("/", sayHelloName) http.HandleFunc("/login", login) http.HandleFunc("/upload", upload) err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal(err) } }
clientupload/main.go
package main import ( "bytes" "fmt" "io" "io/ioutil" "mime/multipart" "net/http" "os" ) func postFile(filename string, targetUrl string) error { bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename) if err != nil { fmt.Println("error writing to buff") return err } fh, err := os.Open(filename) if err != nil { fmt.Println("error opening to buff") return err } defer fh.Close() _, err = io.Copy(fileWriter, fh) if err != nil { fmt.Println("error Copy") return err } contentType := bodyWriter.FormDataContentType() bodyWriter.Close() resp, err := http.Post(targetUrl, contentType, bodyBuf) if err != nil { fmt.Println("error Post") return err } defer resp.Body.Close() resp_body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("error ReadAll") return err } fmt.Println(resp.Status) fmt.Println(string(resp_body)) return nil } func main() { target_url := "http://localhost:9090/upload" filename := "./bee-1.8.3.tar.gz" postFile(filename, target_url) }