dongrouyuan5685 2018-12-30 20:30
浏览 604

尽管表单有效,但ParseMultipartForm总是失败

I am trying to handle uploaded files in go http server. But calling ParseMultipartForm always fails with strange error: "multipart: NextPart: EOF" although the form is valid. Debugging it, I can see that I get the full encoded data, and size and parameters. Yet it fails parsing.

Here is the html form:

<html>
<head>
     <title>Upload file</title>
</head>
<body>
<form enctype="multipart/form-data" action="http://localhost:8080/uploadLogo/" method="post">
    <input type="file" name="uploadfile" />
    <input type="hidden" name="token" value="{{.}}" />
    <input type="submit" value="upload" />
</form>
</body>
</html>

And here is the relevant upload function:

// upload logic
func upload(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method)
    if r.Method == "GET" {
        // crutime := time.Now().Unix()
        // h := md5.New()
        // io.WriteString(h, strconv.FormatInt(crutime, 10))
        // token := fmt.Sprintf("%x", h.Sum(nil))

        // t, _ := template.ParseFiles("upload.gtpl")
        // t.Execute(w, token)
    } else {
        err := r.ParseMultipartForm(5 * 1024 * 1024 * 1024)
        if err != nil {
            fmt.Println("Error ParseMultipartForm: ", err) // here it fails !!! with: "multipart: NextPart: EOF"
            return
        }

        file, handler, err := r.FormFile("uploadfile")
        if err != nil {
            fmt.Println("Error parsing file", err)
            return
        }
        defer file.Close()
        fmt.Fprintf(w, "%v", handler.Header)
        fmt.Println("filename:", handler.Filename)
        f, err := os.OpenFile(logosDir + handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        io.Copy(f, file)
   }
}

Couldn't understand why that happens.

Here is how I start my server:

func uploadLogoHandler(w http.ResponseWriter, r *http.Request) {
    //fmt.Fprintf(w, "viewLogoHandler, Path: %s!", r.URL.Path[1:])
    bodyBytes, _ := ioutil.ReadAll(r.Body)
    bodyString := string(bodyBytes)
    writeToLog("uploadLogoHandler:" + r.URL.Path, "bodyString length:" + strconv.Itoa(len(bodyString)))
    upload(w, r)
}

///////////////////////////////////////////////////////////////////////////////////////
// main

func main() {
    if(len(os.Args) < 2) {
        fmt.Println("Usage: receiver [port number]")
        os.Exit(1)
    }

    port := os.Args[1]

    s := &http.Server{
        Addr:           ":" + port,
        ReadTimeout:    10 * time.Second,
        WriteTimeout:   10 * time.Second,
    }   
    http.HandleFunc("/uploadLogo/", uploadLogoHandler)
    http.HandleFunc("/", handler)
    log.Fatal(s.ListenAndServe())
}
  • 写回答

1条回答 默认 最新

  • doujiang1993 2018-12-30 20:30
    关注

    As writing the question I have already found the problem. It is in the handle function. I read all the stream data before calling the upload function. Here is the revised code of the handler, and now everything works:

    func uploadLogoHandler(w http.ResponseWriter, r *http.Request) {
        writeToLog("uploadLogoHandler:" + r.URL.Path, "")
        upload(w, r)
    }
    

    If I understand correctly so the error: "multipart: NextPart: EOF" means that the form is empty - and it was empty because I emptied the buffer stream before.

    Hope it will help others. Best.

    评论

报告相同问题?

悬赏问题

  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解