Using Node.js to download files
Using Node.js to download files
// Dependencies var fs = require('fs'); var url = require('url'); var http = require('http'); var exec = require('child_process').exec; var spawn = require('child_process').spawn; // App variables var file_url = 'http://upload.wikimedia.org/wikipedia/commons/4/4f/Big%26Small_edit_1.jpg'; var DOWNLOAD_DIR = './downloads/'; // We will be downloading the files to a directory, so make sure it's there // This step is not required if you have manually created the directory var mkdir = 'mkdir -p ' + DOWNLOAD_DIR; var child = exec(mkdir, function(err, stdout, stderr) { if (err) throw err; else download_file_httpget(file_url); }); // Function to download file using HTTP.get var download_file_httpget = function(file_url) { var options = { host: url.parse(file_url).host, port: 80, path: url.parse(file_url).pathname }; var file_name = url.parse(file_url).pathname.split('/').pop(); var file = fs.createWriteStream(DOWNLOAD_DIR + file_name); http.get(options, function(res) { res.on('data', function(data) { file.write(data); }).on('end', function() { file.end(); console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR); }); }); };The above function is probably the best way to download files using HTTP.get in Node. Make a HTTP.get request and create a writable stream using fs.createWriteStream. Since the HTTP.get's response is a stream, it has the data event, which carries the chunks of data sent by the server. One each data event, write the data to the writeable stream. Once the server finishes sending data, close the instance of fs.createWriteStream. If you are trying to use fs.write or fs.writeFile or any of their variants, they will fail for medium to large files. Use fs.createWriteStream instead for reliable results.
沒有留言:
張貼留言