输入类型为文本的值表单(enctype=”multipart/form-data”)返回空值。

huangapple go评论84阅读模式
英文:

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 == &quot;POST&quot; {
		r.ParseForm()
		company := r.FormValue(&quot;company&quot;)
		fmt.Println(company)
		_, header, _ := r.FormFile(&quot;upfile&quot;)
		fmt.Println(header.Filename)
		return
	}
	w.Write([]byte(&quot;&lt;html&gt;&lt;body&gt;&quot;))
	w.Write([]byte(fmt.Sprintf(&quot;&lt;form method=\&quot;POST\&quot; enctype=\&quot;multipart/form-data\&quot;&gt;&quot;)))
	w.Write([]byte(&quot;Enter Company &lt;input type=\&quot;text\&quot; maxlength=\&quot;80\&quot; size=\&quot;80\&quot; name=\&quot;company\&quot; &gt;&lt;br/&gt;&quot;))
	w.Write([]byte(&quot;File to upload: &lt;input type=\&quot;file\&quot; name=\&quot;upfile\&quot; /&gt;&lt;br/&gt;&quot;))
	w.Write([]byte(&quot;&lt;input type=\&quot;submit\&quot;  value=\&quot;Submit\&quot;/&gt;&quot;))
	w.Write([]byte(&quot;&lt;/form&gt;&quot;))
	w.Write([]byte(&quot;&lt;/body&gt;&lt;/html&gt;&quot;))
	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()方法。

Request.FormFile

FormFile返回提供的表单键的第一个文件。FormFile会在必要时调用ParseMultipartForm和ParseForm。

Request.FormValue

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.

Request.FormFile

> FormFile returns the first file for the provided form key. FormFile
> calls ParseMultipartForm and ParseForm if necessary.

Request.FormValue

> 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.

&lt;form action=&quot;/fupload&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
   &lt;input type=&quot;file&quot; name=&quot;fileupload&quot;&gt;
&lt;/form&gt;


file, _, err := req.FormFile(&quot;fileupload&quot;)
	switch err {
	case nil:
		defer file.Close()
		fileData, err := ioutil.ReadAll(file)
        //check err
	case http.ErrMissingFile:
		//do something
	default:
		//do something
	}

huangapple
  • 本文由 发表于 2016年1月30日 03:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35092641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定