Is there any way to track the progress? #34

Closed
dlion opened this Issue Sep 1, 2013 · 8 comments

Comments

Projects
None yet
3 participants

dlion commented Sep 1, 2013

I used needle to upload my file into my server.
There is any way to track upload progress ?

 var data = {
    fileUpload: { file: file, content_type: 'application/octet-stream' }
}; 
needle.post(config.URL+api, data, { multipart: true}, function(err, resp, body) {
...
});
Owner

tomas commented Sep 2, 2013

You mean from the command line or through your app?

dlion commented Sep 2, 2013

My app is a shell app, i want to show a progressbar in my shell like this

############################################################## 100.0%

but how ?
Ho can I check how much bytes I've sent to my server ?

Owner

tomas commented Sep 2, 2013

Currently you can't. I suppose you could fork the repo and trigger an event whenever a chunk of data is sent, so you can calculate the percentage done vs remaining.

dlion commented Sep 2, 2013

Damn, I saw that: https://github.com/IndigoUnited/node-request-progress but this is for request modules.
Dammit.

Owner

tomas commented Sep 2, 2013

Looks like that library is for tracking download progress, not upload progress. For downloads I think that same library might be used with Needle as it is. Just replace the request call with needle and it should work.

dlion commented Sep 2, 2013

You right but I want to show upload progressbar, damn.
So, Can I detect chuck of data sent using needle.post?
It's I think the right way to me.

Owner

tomas commented Dec 4, 2013

Shoot I totally forgot about this, sorry mate.

If you're using a recent version of node, you should be able to set a listener on the returned object (which is the response stream). Something like:

var req = needle.post('http://foo.com/bar', { data: 'bla' }, function(err, resp) {
  console.log('Request finished');
});

req.on('data', function(chunk) {
  console.log('Got ' + chunk.length + ' bytes.');
});

@tomas tomas closed this Mar 5, 2014

fedegratti commented Feb 12, 2018

Hey @tomas , I would love to have this feature. Yesterday I was all day changing my app from request to needle and I could do everything but this. I really need to know at least the bytes transferred while I'm uploading a file. I tried to do something like this (when I was using request it worked), but req.request.connection._bytesDispatched always returns the same value:

  var stats = jetpack.inspect(file.path)
  var needleFile = {
    buffer: jetpack.read(file.path, 'buffer'),
    content_type: file.type,
    filename: file.name
  }

  var data = { 
    parent_id: parentId, 
    file: needleFile 
  }

  var options = {
    follow: 3,
    multipart: true
  }

  var req = needle.post(fileUploadUrl, data, options,
  function(err, resp, body) {
      clearTimeout(pooler)

      if (resp.statusCode == 201) {
        console.log(body)
      } else {
        console.log(resp.statusCode, resp.statusMessage)
      }
  })

  var pooler = setInterval(() => {
    pCb({
        percentage: Math.floor(req.request.connection._bytesDispatched * 100 / stats['size']), 
        eta: ''
    })
  }, 500)

Also I tried using req.on('data', ... but it didn't work. Is there any way to get the progress state when uploading?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment