doumianfeng5065 2013-09-05 15:08 采纳率: 0%
浏览 61

在Golang中访问上传的文件

I'm having issues with accessing files i upload w/ golang. I'm really new to the language and have gone through more than a few attempts-- can't find any answers to this online either.

What am i doing wrong? In this code, i never get to the block where it lists the # of files uploaded.

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

Update

I was able to get the above code to work. Which is great. The answer below shows how to do it async, which may be a better code sample than mine.

  • 写回答

2条回答 默认 最新

  • doujiang1939 2014-07-21 09:27
    关注

    Answer Download the latest golang release.

    I experienced the problem before, using the old golang versions, I do not know what happened, but with the latest golang its working. =)

    My upload handler code below... Full code at: http://noypi-linux.blogspot.com/2014/07/golang-web-server-basic-operatons-using.html

      // parse request  
      const _24K = (1 << 10) * 24  
      if err = req.ParseMultipartForm(_24K); nil != err {  
           status = http.StatusInternalServerError  
           return  
      }  
      for _, fheaders := range req.MultipartForm.File {  
           for _, hdr := range fheaders {  
                // open uploaded  
                var infile multipart.File  
                if infile, err = hdr.Open(); nil != err {  
                     status = http.StatusInternalServerError  
                     return  
                }  
                // open destination  
                var outfile *os.File  
                if outfile, err = os.Create("./uploaded/" + hdr.Filename); nil != err {  
                     status = http.StatusInternalServerError  
                     return  
                }  
                // 32K buffer copy  
                var written int64  
                if written, err = io.Copy(outfile, infile); nil != err {  
                     status = http.StatusInternalServerError  
                     return  
                }  
                res.Write([]byte("uploaded file:" + hdr.Filename + ";length:" + strconv.Itoa(int(written))))  
           }  
      }  
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP