1const http = require('http');
2const fs = require('fs');
3
4const url = 'www.example.com/image.png'; // link to file you want to download
5const path = 'app/assets/my_image_name.xlsx' // where to save a file
6
7const request = http.get(url, function(response) {
8 if (response.statusCode === 200) {
9 var file = fs.createWriteStream(path);
10 response.pipe(file);
11 }
12 request.setTimeout(60000, function() { // if after 60s file not downlaoded, we abort a request
13 request.abort();
14 });
15});
1const http = require('http');
2const fs = require('fs');
3
4const file = fs.createWriteStream("file.jpg");
5const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
6 response.pipe(file);
7});
8
1var http = require('http');
2var fs = require('fs');
3
4var download = function(url, dest, cb) {
5 var file = fs.createWriteStream(dest);
6 var request = http.get(url, function(response) {
7 response.pipe(file);
8 file.on('finish', function() {
9 file.close(cb); // close() is async, call cb after close completes.
10 });
11 }).on('error', function(err) { // Handle errors
12 fs.unlink(dest); // Delete the file async. (But we don't check the result)
13 if (cb) cb(err.message);
14 });
15};
1const http = require('http'); // or 'https' for https:// URLs
2const fs = require('fs');
3
4const file = fs.createWriteStream("file.jpg");
5const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
6 response.pipe(file);
7});