AwesomeDevin / blog

Welcome to Devin's blog,I'm trying to be a fullstack developer and sticking with it !!!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

【Nodejs】复制文件并获取进度

AwesomeDevin opened this issue · comments

const fs = require('fs')

const input = 'test.mp4'
var file = fs.createReadStream(input);
var out = fs.createWriteStream('./test1.mp4');

let totalSize = fs.statSync( input ).size  // 通过 fs.statSync 获取文件大小
let curSize = 0

file.on('data',function(chunk){
  curSize += chunk.length
  const percent = (curSize / totalSize * 100)
  out.write(chunk)
  console.log(`读取中,当前进度:${percent}`)
});
file.on('end',function(){
	out.end();
})