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

发布于 2022-09-07 22:15:25 字数 2003 浏览 12 评论 0

初学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)
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2022-09-14 22:15:25
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
可以直接上传的

深海夜未眠 2022-09-14 22:15:25

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文