duanlianyun0462 2016-10-10 09:20
浏览 37
已采纳

无法使用GOLANG和POLYMER http将上传的文件保存在服务器上:无此文件

I am using vaadin upload to upload files on web application with polymer. And I am using golang for back-end.

<vaadin-upload target="../upload" max-files="20" accept="application/pdf,image/*" 
method="POST"> </vaadin-upload>

I checked that encoding type used in vaadin upload is multipart/form-data. My golang code is below.

func upload(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method)
    if r.Method == "GET" {
        crutime := time.Now().Unix()
        h := md5.New()
        io.WriteString(h, strconv.FormatInt(crutime, 10))
        token := fmt.Sprintf("%x", h.Sum(nil))

        t, _ := template.ParseFiles("upload.gtpl")
        t.Execute(w, token)
    } else {
        r.ParseMultipartForm(32 << 20)
        file, handler, err := r.FormFile("uploadFile")
        if err != nil {
            fmt.Println(err)
            return
        }
        defer file.Close()
        fmt.Fprintf(w, "%v", handler.Header)
        f, err := os.OpenFile("./test/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        io.Copy(f, file)
    }
}

It gives error on server side http: no such file. I checked this error is returned by FormFile when the provided file field name is either not present in the request or not a file field.

How do I correct my form file name. Although everything seems fine on front-end enter image description here

  • 写回答

3条回答 默认 最新

  • dongshao4207 2016-10-11 18:13
    关注

    Answering my own question for those who are facing same problem. Found out some code which can access file without knowing the key value.

    func UploadHandler(res http.ResponseWriter, req *http.Request) {  
          var (  
               status int  
               err  error  
          )  
          defer func() {  
               if nil != err {  
                    http.Error(res, err.Error(), status)  
               }  
          }()  
          // parse request  
         // const _24K = (1 << 20) * 24  
          if err = req.ParseMultipartForm(32 << 20); nil != err {  
               status = http.StatusInternalServerError  
               return  
          } 
            fmt.Println("No memory problem")
          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))))  
               }  
          }  
     } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!