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
"./test/"+handler.Filename
actually produces something suitable for writing (e.g. ./test must exist). – Utilitarianismr.FormFile("value_name")
– Uphemia