The server of Golang cannot receive the file submitted by HTML

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)
    }
}
May.31,2021

package main

import (
    "net/http"
    "io"
    "os"
)

func main() {
    http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
        if r.Method == "GET" {
            w.Header().Set("Content-Type", "text/html;charset=utf-8")
            io.WriteString(w, `<form method="post" action="http://localhost:9090/upload" enctype="multipart/form-data">
<input type="file" name="file"/>
<button type="submit"></button>
</form>`)
            return
        }
        if r.Method == "POST" {
            r.ParseMultipartForm(1024 * 1024 * 10) // 10M
            fp, fileHeader, err := r.FormFile("file")
            if err != nil {
                http.Error(w, err.Error(), 500)
                return
            }
            defer fp.Close()
            localFp, err := os.OpenFile("./test/"+fileHeader.Filename, os.O_WRONLY|os.O_CREATE, 0666)
            if err != nil {
                http.Error(w, err.Error(), 500)
                return
            }
            defer localFp.Close()
            io.Copy(localFp, fp)
            io.WriteString(w, "success")
            return
        }
        http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
    })
    http.ListenAndServe(":9090", nil)
}

clipboard.png
can upload directly


is awkward, I think. You seem to have typed the wrong address:
action= "http//localhost:9090/upload"
should be:
action= "http://localhost:9090/upload"
is missing quotation marks. Or you can use the relative address
action= "/ upload"

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e03a-347a6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e03a-347a6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?