相关疑难解决方法(0)

在Golang中访问上传的文件

我在访问上传w/golang的文件时遇到问题.我对这门语言很陌生并经历了不少尝试 - 无法在网上找到任何答案.

我究竟做错了什么?在这段代码中,我从未到过列出上传文件数量的块.

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Println("handling req...")

  if r.Method =="GET"{
    fmt.Println("GET req...")

  } else {

    //parse the multipart stuff if there
    err := r.ParseMultipartForm(15485760)

    //
    if err == nil{
        form:=r.MultipartForm
        if form==nil {
            fmt.Println("no files...")

        } else {
            defer form.RemoveAll()
            // i never see this actually occur
            fmt.Printf("%d files",len(form.File))
        }
    } else {
        http.Error(w,err.Error(),http.StatusInternalServerError)
        fmt.Println(err.Error())
    }
  }

  //fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
  fmt.Println("leaving...")
}
Run Code Online (Sandbox Code Playgroud)

更新

我能够使上面的代码工作.哪个好.下面的答案显示了如何进行异步,这可能是比我更好的代码示例.

file-upload http go

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

file-upload ×1

go ×1

http ×1