dongzhan8620 2015-03-09 10:42
浏览 63
已采纳

Golang GET多部分表单数据

Let say I have a html form

<form enctype="multipart/form-data" action="http://localhost:8080/incarcat" method="post">
    <input type="file" name="file" multiple="multiple"/>
    <input type="checkbox" name="delete" value="delete">
    <input type="submit" value="Load" />
</form>

and the following go code

read_form, err := req.MultipartReader()
for {
    part, err_part := read_form.NextPart()
    if err_part == io.EOF {
        break
    }
    if part.FormName() == "file" {
        //do something with files
    } else if part.FormName() == "delete" {
        //curata_folder()
        log.Println("delete is: ", part.Header.Get("delete"))
    }
}

How do i get the value of "delete"?

part.Header.Get("delete"))

it's not working

  • 写回答

2条回答 默认 最新

  • douaoj0994 2015-03-09 11:08
    关注

    You can probably content yourself by using the request.ParseMultipartForm method, then use the request.FormValue to get values as usual. Note that you also have the request.MultipartForm to get access to your files.

    Example:

    func(w http.ResponseWriter, r *http.Request) {
        // Here the parameter is the size of the form data that should
        // be loaded in memory, the remaining being put in temporary
        // files
        r.ParseMultipartForm(0)
    
        fmt.Println(r.FormValue("delete"))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongzouche9108 2016-02-28 20:08
    关注

    The part implements the io.Reader interface. I am assuming what you are really asking is how to convert the part reader into a string, in the case that it is not a file. You can do so like this:

    for {
       part, err_part := read_form.NextPart()
       if err_part == io.EOF {
          break
       }
       if part.FormName() == "delete" {
          buf := new(bytes.Buffer)
          buf.ReadFrom(part)
          log.Println("delete is: ", buf.String())
       } 
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法
  • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
  • ¥15 macos13下 ios交叉编译的问题
  • ¥15 bgz压缩文件怎么打开
  • ¥15 封装dll(引入了pcl的点云设计库)
  • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
  • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?