Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js file.download

const http = require('http'); // or 'https' for https:// URLs
const fs = require('fs');

const file = fs.createWriteStream("file.jpg");
const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
  response.pipe(file);
});
Comment

download a file nodejs

const http = require('http');
const fs = require('fs');

const file = fs.createWriteStream("file.jpg");
const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
  response.pipe(file);
});
Comment

nodejs download file

const fs = require('fs');
const http = require('http');

const download = (url, dest, cb) => {
    const file = fs.createWriteStream(dest);

    const request = http.get(url, (response) => {
        // check if response is success
        if (response.statusCode !== 200) {
            return cb('Response status was ' + response.statusCode);
        }

        response.pipe(file);
    });

    // close() is async, call cb after close completes
    file.on('finish', () => file.close(cb));

    // check for request error too
    request.on('error', (err) => {
        fs.unlink(dest, () => cb(err.message)); // delete the (partial) file and then return the error
    });

    file.on('error', (err) => { // Handle errors
        fs.unlink(dest, () => cb(err.message)); // delete the (partial) file and then return the error
    });
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: eventemitter in angular 
Javascript :: min heap javascript 
Javascript :: .scrollLeft + 1, 0 
Javascript :: how to deep copy an object in javascript 
Javascript :: javascript find object array 
Javascript :: knexjs search uppercase 
Javascript :: how to pass data in body of delete request angular 
Javascript :: delete element html javascript 
Javascript :: react native password meter 
Javascript :: remover ultimo character string javascript 
Javascript :: angular read config from json 
Javascript :: react google maps 
Javascript :: react native navigation nested 
Javascript :: queryselectorall in jquery 
Javascript :: check object has key 
Javascript :: parse integer in javascript 
Javascript :: angular formgroup on value change 
Javascript :: Creating URL Search Parameters From An Array 
Javascript :: node ssh 
Javascript :: each jquery 
Javascript :: The document.getElementById() Method 
Javascript :: then js 
Javascript :: for javascript 
Javascript :: accept 2 values after decimal in angular forms 
Javascript :: jquery selector id ends with 
Javascript :: how to create thumbnail image from video in javascript 
Javascript :: delete last character from string js 
Javascript :: javascript remove duplicate objects from array es6 
Javascript :: display time and date in javascript 
Javascript :: javascript function return multiple 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =