How do i track the upload progress in golang

go, http, progress, upload

Solution

This will work, a stream to calc the bytes read and the total progress you need to point the stream somewhere, in this code example I pointed it to a file.

func Upload(w http.ResponseWriter, req *http.Request) {

    mr, err := req.MultipartReader()
    if err != nil {
        return
    }
    length := req.ContentLength
    for {

        part, err := mr.NextPart()
        if err == io.EOF {
            break
        }
        var read int64
        var p float32
        dst, err := os.OpenFile("dstfile", os.O_WRONLY|os.O_CREATE, 0644)
        if err != nil {
            return
        }
        for {
            buffer := make([]byte, 100000)
            cBytes, err := part.Read(buffer)
            if err == io.EOF {
                break
            }
            read = read + int64(cBytes)
            //fmt.Printf("read: %v \n",read )
            p = float32(read) / float32(length) *100
            fmt.Printf("progress: %v \n",p )
            dst.Write(buffer[0:cBytes])
        }
    }
}

Problem

i'm trying to track the upload progress in GOLANG, that's what i got at the moment ``` func Upload(w http.ResponseWriter, req *http.Request) { mr, err := req.MultipartReader() if err != nil { return } for { // var part *multipart.Part part, err := mr.NextPart() mr.partsRead if err == io.EOF { break } println(part) } } ```

Original source