0

I want to read file with stream to update UI progress bar. But after I start experimenting with fs.createReadStream I noticed that I receive different results. For example:

var d = fs.readFileSync('file.bin', null);
console.log(d);
console.log(d.byteLength);

Returns in console:

Buffer(188000)

188000

But this code:

var rd = fs.createReadStream('file.bin');
var data = '';
rd.on("error", function(err) {
    console.log(err);
});
rd.on("data", function(chunk) {
    console.log(chunk.length);
    data += chunk;
});
rd.on("end", function() {
    console.log('There will be no more data.');
    var buf = Buffer.from(data, null);
    console.log(buf)
    console.log(buf.byteLength)
});

Returns in console:

Uint8Array(297684)

297684

Why stream returns Uint8Array instead of Buffer object and why they have different sizes

Arti
  • 7,356
  • 12
  • 57
  • 122

0 Answers0