Gautham Badhrinathan Gautham Badhrinathan - 2 months ago 13
CoffeeScript Question

Upload Progress — Request

I'm uploading a file using

.

req = request.post url: "http://foo.com", body: fileAsBuffer, (err, res, body) ->
console.log "Uploaded!"


How do I know how much data has actually been uploaded? Is there some event that I can subscribe to or is there a property of the
request
that I can poll?

If none, what would be the best approach to upload data and know how much has been uploaded?

Answer

I needed a handle on the upload progress for yet another project of mine.

What I found out is that you can poll the request's connection._bytesDispatched property.

For example:

r = request.post url: "http://foo.com", body: fileAsBuffer
setInterval (-> console.log "Uploaded: #{r.req.connection._bytesDispatched}"), 250

Note: If you were piping to r, poll r.req.connection.socket._bytesDispatched instead.

Comments