I want to download file from nodejs and write it. Currently, im using axios and createWriteStream. It writes fine. But i want to handle write when file is large. When the file is larger than 5mb, i want it return message " file is large" and prevent writing file.
How do i handle large file in createWriteStream? thanks you.
here is my code:
async function downloadImage () {
const url = 'https://example.com/file.zip'
const path = Path.resolve(__dirname, 'files', 'file.zip')
const writer = Fs.createWriteStream(path)
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', () => {
resolve({ path })
})
writer.on('error', () => {
fs.unlink(path)
reject(new Error('Cant download!'))
})
})
}