Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 :: scroll to div js 
Javascript :: javascript check image src 
Javascript :: javascript function loop through array 
Javascript :: Access to XMLHttpRequest has been blocked by CORS policy 
Javascript :: disable a button react 
Javascript :: creating array of objects usinng reduce js 
Javascript :: Animated: `useNativeDriver` was not specified. This is a required option and must be explicitly set to `true` or `false` 
Javascript :: discord.js bot mention 
Javascript :: add value to each object in array javascript 
Javascript :: ascii code js 
Javascript :: pyspark from_json example 
Javascript :: react hook form with yup resolver 
Javascript :: select text with javascript 
Javascript :: get last two digits of year javascript 
Javascript :: js print array without comma 
Javascript :: gsap pin scrolltrigger 
Javascript :: how to get single element from nested array mongoose 
Javascript :: mongodb aggregate node.js 
Javascript :: javascript two digits number 
Javascript :: df.saveto json 
Javascript :: object deep copy 
Javascript :: check if two rectangles overlap javascript canvas 
Javascript :: javascript loop over the alphabet and return the vowels 
Javascript :: number round 
Javascript :: how to check value is array or not in javascript 
Javascript :: get Two digit number js 
Javascript :: bootstrap datepicker options 
Javascript :: dynamic loop variable .each create hash javascript 
Javascript :: include other js files in a js file 
Javascript :: change data js 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =