Advertisement
krovn

Untitled

Nov 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io"
  6.     "io/ioutil"
  7.     "net/http"
  8. )
  9.  
  10. func main() {
  11.     http.HandleFunc("/", index)
  12.     http.Handle("/favicon.ico", http.NotFoundHandler())
  13.     http.ListenAndServe(":8080", nil)
  14. }
  15.  
  16. func index(res http.ResponseWriter, req *http.Request) {
  17.     var s string
  18.     fmt.Println(req.Method)
  19.     if req.Method == http.MethodPost {
  20.         f, h, err := req.FormFile("q")
  21.         if err != nil {
  22.             http.Error(res, err.Error(), http.StatusInternalServerError)
  23.             return
  24.         }
  25.         defer f.Close()
  26.         fmt.Println("\nfile: ", f, "\nheader: ", h, "\nerr", err)
  27.         bs, err := ioutil.ReadAll(f)
  28.         if err != nil {
  29.             http.Error(res, err.Error(), http.StatusInternalServerError)
  30.             return
  31.         }
  32.         s = string(bs)
  33.     }
  34.     res.Header().Set("Content-Type", "text/html; charset=utf-8")
  35.     io.WriteString(res, `
  36.         <form method="POST" enctype="multipart/form-data">
  37.             <input type="file" name="q" />
  38.             <input type="submit" />
  39.         </form>
  40.     `+s)
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement