I have an app for upload files to my server, I am using fs and stream my file to the server and use Chuck data for keep tracking file upload progress.
Here sample code for uploading:
// upload video
function uploadVideo(url,file,callback) {
let size = fs.lstatSync(file).size;
let bytes = 0;
let formData = {
file: fs.createReadStream(file).on('data', (chunk) => {
bytes += chunk.length
process.stdout.write(file+filesize(bytes).human()+' of '+filesize(size).human()+' Uploaded'+"\r")
}),
};
request.post({url:url, json:true, formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return callback('upload failed:', err);
}
callback(null,body);
});
}
as you can see I am using process.stdout.write for showing upload progress without newline and its work fine.
The problem I have when I am uploading multiple files same time, all upload progress get override in one line, I want each upload progress be in separate line.
Multiple upload example:
let files = ['video-1.mp4','video-2.mp4','video-3.mp4','video-4.mp4','video-5.mp4']
// upload multiple file
files.forEach((file) => {
uploadVideo(url,file,function(err,output){
console.log(output)
})
})