I'm uploading a file using Request
.
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?
I spent a couple of hours to find anything valid in
request
andnode
sources, and finally found a different approach, which feels more correct to me.We can rely on
drain
event andbytesWritten
property:Alternatively if you need to handle progress of file bytes, it's easier to use stream
data
event:Stream buffer size is
65536
bytes and read/drain procedure runs iteratively.This seems to be working pretty well for me with
node v4.5.0
andrequest v2.74.0
.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
'sconnection._bytesDispatched
property.For example:
Note: If you were piping to
r
, pollr.req.connection.socket._bytesDispatched
instead.Someone has created a nice module to accomplish this that has been running in the production stack for transloadit (so it is dependable and well maintained). You can find it here:
https://github.com/felixge/node-formidable
Code should look like:
You could then push status to a client using Socket.io
Interesting Note: This was one of the problems that led to Node's creation. In this video Ryan talks about how node was started trying to find the best way of notifying a user, real time, about the status of file upload over the web...anyway I digress, but the video is worth a watch if you are interested in Node's history