英文:
Input TYPE TEXT Value form (enctype=“multipart/form-data”) returns null
问题
func fupload(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
company := r.FormValue("company")
fmt.Println(company)
_, header, _ := r.FormFile("upfile")
fmt.Println(header.Filename)
return
}
w.Write([]byte("<html><body>"))
w.Write([]byte(fmt.Sprintf("<form method=\"POST\" enctype=\"multipart/form-data\">")))
w.Write([]byte("Enter Company <input type=\"text\" maxlength=\"80\" size=\"80\" name=\"company\" ><br/>"))
w.Write([]byte("File to upload: <input type=\"file\" name=\"upfile\" /><br/>"))
w.Write([]byte("<input type=\"submit\" value=\"Submit\"/>"))
w.Write([]byte("</form>"))
w.Write([]byte("</body></html>"))
return
}
对于输入类型为文本(例如:公司名称),当 enctype="multipart/form-data" 时,始终返回 NULL。
英文:
func fupload(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
company := r.FormValue("company")
fmt.Println(company)
_, header, _ := r.FormFile("upfile")
fmt.Println(header.Filename)
return
}
w.Write([]byte("<html><body>"))
w.Write([]byte(fmt.Sprintf("<form method=\"POST\" enctype=\"multipart/form-data\">")))
w.Write([]byte("Enter Company <input type=\"text\" maxlength=\"80\" size=\"80\" name=\"company\" ><br/>"))
w.Write([]byte("File to upload: <input type=\"file\" name=\"upfile\" /><br/>"))
w.Write([]byte("<input type=\"submit\" value=\"Submit\"/>"))
w.Write([]byte("</form>"))
w.Write([]byte("</body></html>"))
return
}
For the input type Text (eg) Company here always return NULL, when enctype="multipart/form-data"
答案1
得分: 2
ParseForm
只解析查询参数。根据文档:
ParseForm解析URL中的原始查询并更新r.Form。
对于POST或PUT请求,它还将请求体解析为表单,并将结果放入r.PostForm和r.Form中。POST和PUT请求体参数优先于r.Form中的URL查询字符串值。
如果请求体的大小尚未被MaxBytesReader限制,则大小限制为10MB。
ParseMultipartForm会自动调用ParseForm。它是幂等的。
如果要处理"multipart/form-data",请使用ParseMultipartForm
,否则不要调用任何一个函数,让FormValue
自行处理所需的内容。
英文:
ParseForm
only parses the query parameters. From the docs:
> ParseForm parses the raw query from the URL and updates r.Form.
>
> For POST or PUT requests, it also parses the request body as a form
> and put the results into both r.PostForm and r.Form. POST and PUT body
> parameters take precedence over URL query string values in r.Form.
>
> If the request Body's size has not already been limited by
> MaxBytesReader, the size is capped at 10MB.
>
> ParseMultipartForm calls ParseForm automatically. It is idempotent.
Either use ParseMultipartForm
if you want to handle "multipart/form-data", or don't call either and let FormValue
call what's needed.
答案2
得分: 0
是的,你应该使用enctype="multipart/form-data"。但是如果你已经使用了FormValue(key string)或FormFile(key string)方法,就不应该使用ParseForm()方法。
FormFile返回提供的表单键的第一个文件。FormFile会在必要时调用ParseMultipartForm和ParseForm。
FormValue返回查询的命名组件的第一个值。POST和PUT请求体参数优先于URL查询字符串值。FormValue会在必要时调用ParseMultipartForm和ParseForm,并忽略这些函数返回的任何错误。如果键不存在,FormValue返回空字符串。要访问同一键的多个值,请调用ParseForm,然后直接检查Request.Form。
<form action="/fupload" method="POST" enctype="multipart/form-data">
<input type="file" name="fileupload">
</form>
file, _, err := req.FormFile("fileupload")
switch err {
case nil:
defer file.Close()
fileData, err := ioutil.ReadAll(file)
//检查错误
case http.ErrMissingFile:
//做一些处理
default:
//做一些处理
}
英文:
Yes, you should use enctype="multipart/form-data". But you should not use ParseForm() method if you already use FormValue(key string) or FormFile(key string) method.
> FormFile returns the first file for the provided form key. FormFile
> calls ParseMultipartForm and ParseForm if necessary.
> FormValue returns the first value for the named component of the
> query. POST and PUT body parameters take precedence over URL query
> string values. FormValue calls ParseMultipartForm and ParseForm if
> necessary and ignores any errors returned by these functions. If key
> is not present, FormValue returns the empty string. To access multiple
> values of the same key, call ParseForm and then inspect Request.Form
> directly.
<form action="/fupload" method="POST" enctype="multipart/form-data">
<input type="file" name="fileupload">
</form>
file, _, err := req.FormFile("fileupload")
switch err {
case nil:
defer file.Close()
fileData, err := ioutil.ReadAll(file)
//check err
case http.ErrMissingFile:
//do something
default:
//do something
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论