when learning Golang as a server, I now encounter a problem: when using HTML to submit a file, the browser can get the html, sent by the server, but if it is submitted locally, the browser will be located as the home page. The specific problem is marked in the code, please help solve it, thank you!
this is the html file sent by the server:
import (
"crypto/md5"
"fmt"
"html/template"
"io"
"net/http"
"os"
"strconv"
"time"
)
func upload(w http.ResponseWriter, r *http.Request) {
fmt.Println("method: ", r.Method) //
if r.Method == "GET" {
curtime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(curtime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("upload.html")
t.Execute(w, token)
fmt.Println("upload get") //
} else {
fmt.Println("upload post") // 404
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("./test/"+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("/upload", upload)
err := http.ListenAndServe(":9090", nil)
if err != nil {
fmt.Println(err)
}
}