Created
February 5, 2011 14:20
-
-
Save tommedema/812484 to your computer and use it in GitHub Desktop.
This file contains 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
//fired when request has been answered | |
httpRequest.on('response', function (response) { | |
//set encoding | |
response.setEncoding('utf8'); | |
//setup output stream | |
var filePath = 'someFile.csv'; | |
console.log('Now writing: ' + filePath); | |
var writeStream = fs.createWriteStream(filePath, { | |
'flags': 'w+', | |
'encoding': 'utf8', | |
'mode': 0666 | |
}); | |
writeStream.on('error', function(error) { | |
sys.debug('Error while writing: ' + error); | |
process.exit(); | |
}); | |
//fired on chunk data receival | |
response.on('data', function (chunk) { | |
//write to output stream | |
writeStream.write(chunk); | |
}); | |
//fired when no more data will be received | |
response.on('end', function() { | |
console.log("Download was successful, saved to file. Checking queue again soon."); | |
//close output stream when ready | |
writeStream.on('drain', function() { | |
writeStream.removeAllListeners('drain'); | |
writeStream.end(); //THIS CAUSES BAD FILE DESCRIPTOR ERROR | |
writeStream.destroy(); //THIS CAUSES BAD FILE DESCRIPTOR ERROR | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment