Golang GET 多部分表单数据

假设我有一个 html 表单


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

和以下代码


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"))

    }

}

我如何获得“删除”的价值?


part.Header.Get("delete"))

它不工作


慕沐林林
浏览 200回答 2
2回答

侃侃尔雅

您可能可以通过使用request.ParseMultipartForm方法满足自己,然后request.FormValue像往常一样使用来获取值。请注意,您还request.MultipartForm可以访问您的文件。例子:func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // Here the parameter is the size of the form data that should&nbsp; &nbsp; // be loaded in memory, the remaining being put in temporary&nbsp; &nbsp; // files&nbsp; &nbsp; r.ParseMultipartForm(0)&nbsp; &nbsp; fmt.Println(r.FormValue("delete"))}

冉冉说

该部分实现了 io.Reader 接口。我假设您真正要问的是如何将部分阅读器转换为字符串,以防它不是文件。你可以这样做:for {&nbsp; &nbsp;part, err_part := read_form.NextPart()&nbsp; &nbsp;if err_part == io.EOF {&nbsp; &nbsp; &nbsp; break&nbsp; &nbsp;}&nbsp; &nbsp;if part.FormName() == "delete" {&nbsp; &nbsp; &nbsp; buf := new(bytes.Buffer)&nbsp; &nbsp; &nbsp; buf.ReadFrom(part)&nbsp; &nbsp; &nbsp; log.Println("delete is: ", buf.String())&nbsp; &nbsp;}&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go