1

i have a form with many options to post, and post files with slice,but in Go, Request.ParseForm(),only get the first file, how should i resolve with file slice?

in html

<form enctype="multipart/form-data" method="POST" action="/homeworks" >
  {{if .success}}
    <p>flash success</p>
  {{end}}

  <div id="postform">
    本次作业标题
      <input type="text" name="title" />
      <br>
    <div class="postoption"> 
      添加项目
      <input type="text" name="option[]" />
      音频文件
      <input type="file" name="radio[]" />
      答案
      <input type="text" name="answer[]" />
    </div>
  </div>

  <input type="submit" value="提交" />
</form>

if i do like

    file,header,err:=r.FormFile("file")
    fmt.Println(header)
    if err!=nil{
        panic(err)
    }

it will panic no such file, how can i get files slice. if i change it to radio ,it works,but
can not get file slice.

4
  • 1
    you can try getting a MultipartReader for the request and then read the parts manually. golang.org/pkg/net/http/#Request.MultipartReader Commented Feb 27, 2014 at 12:37
  • 2
    shouldnt it be <input type="file" name="file" />? Commented Feb 27, 2014 at 13:28
  • 1
    @Uriel_SVK is right. The string argument to FormFile is the name of the input field defined in html. Commented Feb 27, 2014 at 15:16
  • @Not_a_Golfer could you give me an example? Commented Feb 28, 2014 at 11:09

1 Answer 1

1

that's finally how i deal with it, By reading Go source code of formfile()

fhs := r.MultipartForm.File["radio"]

fhs are the Headers of FileHeader of mutlipart .

by useing Open method, i can get the interface file

for i:=0;i<len(fhs);i++{
    f,err:=fhs[i].Open()
}

then i can do the next steps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.