Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onUploadProgress & onDownloadProgress for node #1793

Closed
alxhotel opened this issue Sep 18, 2018 · 7 comments · Fixed by #3771
Closed

onUploadProgress & onDownloadProgress for node #1793

alxhotel opened this issue Sep 18, 2018 · 7 comments · Fixed by #3771
Projects
Milestone

Comments

@alxhotel
Copy link

alxhotel commented Sep 18, 2018

It would be useful to have the feature of onUploadProgress and onDownloadProgress also in NodeJS.

Example:

const fs = require('fs');
const axios = require('axios');

var data = fs.readFileSync('test.txt');
axios.post('https://example.com/upload', data, {
  onUploadProgress: function (progressEvent) {
    console.log(Math.floor((progressEvent.loaded * 100) / progressEvent.total));
  }
}).then(function (response) {
  console.log(response);
}).catch(function (error) {
  console.log(error);
});
@RikkiGibson
Copy link
Contributor

RikkiGibson commented Sep 23, 2018

I wrote an adapter for axios to provide uniform behavior for these handlers among other things. You may benefit from seeing the code for it:

https://github.com/Azure/ms-rest-js/blob/3c131b37/lib/axiosHttpClient.ts#L103

BTW, your code may work better with fs.createReadStream as you won't have to wait for the file to be completely read before starting to upload it.

@alxhotel
Copy link
Author

Thanks @RikkiGibson !

Although I'm trying the code and it feels like the onUploadProgress is only triggered when reading the file and not in the actual uploading, right?

@RikkiGibson
Copy link
Contributor

That's right. It's not perfectly accurate, but convenient to implement. I think the discrepancy between the amount of file content read and the amount of content received so far by the server will be at most the socket.highWaterMark (defaults to 16 KB).

In case you find it interesting, node.js has built in backpressure mechanisms to prevent the system from running out of memory e.g. when the disk is faster than the network on a large upload, or the network is faster than the disk on a large download.

@alxhotel
Copy link
Author

Ah got it. Still in my case the differences is seems to be bigger than 16KB. I guess there isn't an easy solution for this.

@jacargentina
Copy link

@chinesedfan any advance on this ?

@jasonsaayman
Copy link
Member

Please check if this has been fixed with #3694

@marek-siemieniuk-morawski

Hi @jasonsaayman , I've checked and the problem still occurs.

@jasonsaayman jasonsaayman linked a pull request Apr 29, 2021 that will close this issue
@jasonsaayman jasonsaayman added this to the v0.22.0 milestone Apr 29, 2021
@jasonsaayman jasonsaayman added this to To do in v0.22.0 via automation Apr 29, 2021
v0.22.0 automation moved this from To do to Done Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v0.22.0
  
Done
Development

Successfully merging a pull request may close this issue.

6 participants