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

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

Input TYPE TEXT Value form (enctype=“multipart/form-data”) returns null

问题

  1. func fupload(w http.ResponseWriter, r *http.Request) {
  2. if r.Method == "POST" {
  3. r.ParseForm()
  4. company := r.FormValue("company")
  5. fmt.Println(company)
  6. _, header, _ := r.FormFile("upfile")
  7. fmt.Println(header.Filename)
  8. return
  9. }
  10. w.Write([]byte("<html><body>"))
  11. w.Write([]byte(fmt.Sprintf("<form method=\"POST\" enctype=\"multipart/form-data\">")))
  12. w.Write([]byte("Enter Company <input type=\"text\" maxlength=\"80\" size=\"80\" name=\"company\" ><br/>"))
  13. w.Write([]byte("File to upload: <input type=\"file\" name=\"upfile\" /><br/>"))
  14. w.Write([]byte("<input type=\"submit\" value=\"Submit\"/>"))
  15. w.Write([]byte("</form>"))
  16. w.Write([]byte("</body></html>"))
  17. return
  18. }

对于输入类型为文本(例如:公司名称),当 enctype="multipart/form-data" 时,始终返回 NULL。

英文:
  1. func fupload(w http.ResponseWriter, r *http.Request) {
  2. if r.Method == &quot;POST&quot; {
  3. r.ParseForm()
  4. company := r.FormValue(&quot;company&quot;)
  5. fmt.Println(company)
  6. _, header, _ := r.FormFile(&quot;upfile&quot;)
  7. fmt.Println(header.Filename)
  8. return
  9. }
  10. w.Write([]byte(&quot;&lt;html&gt;&lt;body&gt;&quot;))
  11. w.Write([]byte(fmt.Sprintf(&quot;&lt;form method=\&quot;POST\&quot; enctype=\&quot;multipart/form-data\&quot;&gt;&quot;)))
  12. 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;))
  13. w.Write([]byte(&quot;File to upload: &lt;input type=\&quot;file\&quot; name=\&quot;upfile\&quot; /&gt;&lt;br/&gt;&quot;))
  14. w.Write([]byte(&quot;&lt;input type=\&quot;submit\&quot; value=\&quot;Submit\&quot;/&gt;&quot;))
  15. w.Write([]byte(&quot;&lt;/form&gt;&quot;))
  16. w.Write([]byte(&quot;&lt;/body&gt;&lt;/html&gt;&quot;))
  17. return
  18. }

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。

  1. <form action="/fupload" method="POST" enctype="multipart/form-data">
  2. <input type="file" name="fileupload">
  3. </form>
  1. file, _, err := req.FormFile("fileupload")
  2. switch err {
  3. case nil:
  4. defer file.Close()
  5. fileData, err := ioutil.ReadAll(file)
  6. //检查错误
  7. case http.ErrMissingFile:
  8. //做一些处理
  9. default:
  10. //做一些处理
  11. }
英文:

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.

  1. &lt;form action=&quot;/fupload&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
  2. &lt;input type=&quot;file&quot; name=&quot;fileupload&quot;&gt;
  3. &lt;/form&gt;
  4. file, _, err := req.FormFile(&quot;fileupload&quot;)
  5. switch err {
  6. case nil:
  7. defer file.Close()
  8. fileData, err := ioutil.ReadAll(file)
  9. //check err
  10. case http.ErrMissingFile:
  11. //do something
  12. default:
  13. //do something
  14. }

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:

确定