Engineer bro!

Wed Dec 08 2021 (2 years ago)

Engineer by mistake!

I am a software engineer by passion

How to download an image from URL in nodejs

In this article, we'll learn that how can we download images from a remote URL. After downloading the image, we will encode it in base64 format.

const { Buffer } = require("buffer");
const axios = require("axios");
let url = ",f_auto,fl_progressive,h_320,q_auto,w_320/";

async function downloadImage(url){
    // download the raw image file
    const raw = await axios.get(url, {
        responseType: "arraybuffer",
    // create a base64 encoded string
    let base64 = Buffer.from(, "binary").toString("base64");
    // create image src
    let image = `data:${raw.headers["content-type"]};base64,${base64}`;
    // actual image 
    let imgFile = `<img src="${image}"/>`;

© 2021 dsabyte. All rights reserved