Html 尽管表单有效,但ParseMultipartForm始终失败
我正在尝试处理go http服务器中上载的文件。但是调用ParseMultipartForm总是失败,并出现奇怪的错误:“multipart:NextPart:EOF”,尽管表单是有效的。通过调试,我可以看到我得到了完整的编码数据、大小和参数。但它无法解析 以下是html表单:Html 尽管表单有效,但ParseMultipartForm始终失败,html,http,go,file-upload,Html,Http,Go,File Upload,我正在尝试处理go http服务器中上载的文件。但是调用ParseMultipartForm总是失败,并出现奇怪的错误:“multipart:NextPart:EOF”,尽管表单是有效的。通过调试,我可以看到我得到了完整的编码数据、大小和参数。但它无法解析 以下是html表单: <html> <head> <title>Upload file</title> </head> <body> <form enct
<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>
我不明白为什么会这样
以下是我如何启动服务器:
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())
}
func uploadLogoHandler(w http.ResponseWriter,r*http.Request){
//fmt.Fprintf(w,“viewLogoHandler,路径:%s!”,r.URL.Path[1:]
bodyBytes,u:=ioutil.ReadAll(r.Body)
bodyString:=字符串(bodyBytes)
writeToLog(“uploadLogoHandler:+r.URL.Path,”bodyString长度:+strconv.Itoa(len(bodyString)))
上传(w,r)
}
///////////////////////////////////////////////////////////////////////////////////////
//主要
func main(){
if(len(os.Args)<2){
fmt.Println(“用法:接收器[端口号]”)
操作系统退出(1)
}
端口:=os.Args[1]
s:=&http.Server{
地址:“:”+端口,
读取超时:10*次。秒,
写入时间:10*次。秒,
}
http.HandleFunc(“/uploadLogo/”,uploadLogoHandler)
http.HandleFunc(“/”,handler)
log.Fatal(s.ListenAndServe())
}
在写问题时,我已经发现了问题所在。它在handle函数中。在调用upload函数之前,我读取了所有流数据。下面是处理程序的修订代码,现在一切正常:
func uploadLogoHandler(w http.ResponseWriter, r *http.Request) {
writeToLog("uploadLogoHandler:" + r.URL.Path, "")
upload(w, r)
}
如果我理解正确,那么错误:“multipart:NextPart:EOF”意味着表单是空的——它是空的,因为我之前清空了缓冲流
希望它能帮助别人。
最好的
func uploadLogoHandler(w http.ResponseWriter, r *http.Request) {
writeToLog("uploadLogoHandler:" + r.URL.Path, "")
upload(w, r)
}