a1exlism
11/22/2016 - 7:59 AM

http.get & write files with nodejs

http.get & write files with nodejs

//	File Downloads
const fs = require('fs');
const http = require('http');

function downloadFile(imageUrl, fileName) {
	// var writer = fs.createWriteStream(localFile);

	http.get(imageUrl + fileName, (res) => {
		var imgData = '';
		res.setEncoding('binary');
		res.on('data', (chunk) => {
			imgData += chunk;
		});
		res.on('end', () => {
			fs.writeFile(fileName, imgData, 'binary', (err) => {
				if (err) throw err
				console.log('File saved.');
			});
		})
	});

}


function main(argv) {
	downloadFile('http://test.com/netSec/filedown.php?filename=', argv[0]);
}

main(process.argv.slice(2));