Golang的服务端无法接收HTML提交的文件

初学Golang做服务端,现在遇到了一个问题:当使用HTML提交文件的时候,浏览器可以获取服务器发送的html,但是如果本地提交的话,到浏览器端会被定位成首页,具体问题在代码中标出了,请会的同学帮忙解决一下,谢谢!

这是服务端发送的html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
</head>
<body>
<form enctype="multipart/form-data" action="http//localhost:9090/upload" method="post">
    <input type="file" name="uploadfile"/>
    <input type="hidden" name="token" value="{{.}}"/>
    <input type="submit" value="upload"/>
</form>
</body>
</html>

这是代码,还有其他的(数据库、一些校验等),我在这里都省略了,只保留了出问题的部分,能直接运行:

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)
    }
}
阅读 2.9k
2 个回答
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
可以直接上传的

很尴尬,我觉得吧...你好像地址打错了:
action="http//localhost:9090/upload"
应该是:
action="http://localhost:9090/upload"
少了引号。。或者你用相对地址好点
action="/upload"

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题