Last active
February 20, 2023 08:09
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
}); | |
}); | |
} |
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
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
Thank you so much. It helped me a lot