I have a very simple web server in Go, and on one page there is a form to upload a file:
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*"><br>
Description: <input type="text" name="descr"><br>
<input type="submit">
</form>
And I read the file in Go like this:
func imageUpload(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
_,imageHead,err := r.FormFile("image")
if err != nil {
log.Printf("Error with upload: %s", err)
return
}
imageDesc := r.FormValue("descr")
fileSize := .... ???
log.Printf("Recibí %s <%s> (%d bytes)", imageHead.Filename, imageDesc, fileSize)
}
But what do I do to get fileSize
?
imageData
is a multipart.File , which exposes io.Reader, io.ReaderAt, io.Seeker and io.Closer . But don't file directly? So the only option is to read the io.Reader to see how many bytes I have? Or is there another more efficient option?