Created
March 3, 2011 23:24
-
-
Save mikegerwitz/853846 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require( 'fs' ), | |
// size in bytes | |
CHUNK_SIZE = 50, | |
file = fs.open( 'file', 'r', '0666', function( err, fd ) | |
{ | |
if ( err ) | |
{ | |
console.log( err ); | |
return; | |
} | |
doRead( fd, function() | |
{ | |
console.log( 'File has been read.' ); | |
}); | |
}); | |
function doRead( fd, callback ) | |
{ | |
var data = new Buffer( CHUNK_SIZE ); | |
fs.read( fd, data, 0, CHUNK_SIZE, null, function( err, bytesRead ) | |
{ | |
if ( err ) | |
{ | |
// do something | |
} | |
// your HTTP stuff here. Data is in the 'data' buffer | |
console.log( data.toString().substr( 0, bytesRead ) ); | |
// recursively read | |
if ( bytesRead > 0 ) | |
{ | |
doRead( fd, callback ); | |
} | |
else | |
{ | |
callback(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment