Alec Gorge Alec Gorge - 1 year ago 371
Javascript Question

How to read binary files byte by byte in Node.js

What is the best way to read part of a binary file in Node.js?

I am looking to either access specific bytes in the "header" (less than the first 100 bytes) or read the file byte by byte.

Answer Source

Here is an example of the first 100 bytes from a file descriptor returned by

var fs = require('fs');'file.txt', 'r', function(status, fd) {
    if (status) {
    var buffer = new Buffer(100);, buffer, 0, 100, 0, function(err, num) {
        console.log(buffer.toString('utf8', 0, num));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download