Uploading image from url using Node js

  • Posted by Sharad Jaiswal

    Simple function In Node.js to save an image from URL to local disk/ server

    Here we are going to see how to Upload an image from URL in Node Js. Whenever we use login with facebook, google or any other social media in node js app, we get some basic information like name, email and profile picture from there. In this post, I will show you how to save google, facebook profile picture to the server with node.js.

    Also, Read Node js interview questions

    var fs = require('fs');
    var https = require('https');
    //Node.js Function to save image from External URL.
    function saveImageToDisk(url, localPath) {var fullUrl = url;
    var file = fs.createWriteStream(localPath);
    var request = https.get(url, function(response) {
    response.pipe(file);
    });
    
    }
    

    Usages

    exports.saveImage(req, res) {
    
    let image_path='./uploads/profile/'+Date.now()+'.jpg';
    fetchImage(req.body.profile_pic_url,image_path);
    
    }
    
    
    

    Also, Read Best Node JS Interview Questions

Please Login or Register to leave a response.

Related Articles

Node Js Tutorials

How to enable cors in Node js

In this article, we are going to see how to enable CORS ( Cross-Origin Resource Sharing ) in Node JS. CORS essentially means cross-domain requests. Cross-Origin Resource Sharing (CORS) is a mechanism ..

Node Js Tutorials

Scheduling CRON Jobs on Node js

Cron Jobs are used for scheduling tasks to run on the server.CRON Jobs are the most commonly used method to automate tasks on Server. In this article, we will see how to schedule Jobs in Node.js. We a..

Node Js Tutorials

Dropping an existing index from MongoDB or mongoose

While working with MongoDB and Node js I face an issue. I have created mobile_no unique in starting and after some time I realized that I don't need my mobile_no to be unique. so I just go to my mode..