Skip to content

Instantly share code, notes, and snippets.

@csotiriou
Last active February 20, 2023 08:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csotiriou/2f9b721606ce72b4da70386bb03ca9ca to your computer and use it in GitHub Desktop.
Save csotiriou/2f9b721606ce72b4da70386bb03ca9ca to your computer and use it in GitHub Desktop.
Proper way of downloading a large file using NodeJS and Axios (NodeJS 8 and earlier)
export async function downloadFile(fileUrl: string, outputLocationPath: string) {
const writer = createWriteStream(outputLocationPath);
return Axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then(response => {
//ensure that the user can call `then()` only when the file has
//been downloaded entirely.
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
//no need to call the reject here, as it will have been called in the
//'error' stream;
});
});
});
}
@rikusen0335
Copy link

Thank you so much. It helped me a lot

@csotiriou
Copy link
Author

Thank you so much. It helped me a lot

Thanks! If you use NodeJS 10+, take a look at a much simpler method here: https://gist.github.com/csotiriou/93822047febd7a5c018d2fab1ffcb8c0 - which essentially is the same thing, only using Node's more modern approach

@rikusen0335
Copy link

Thank you for that too!
Sadly for the limitation I use node8 for now, but I'll try that if I use node10+ next time!

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