DEV Community

Rahul kumar
Rahul kumar

Posted on • Originally published at dsabyte.com

3 1

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.

original post

const { Buffer } = require("buffer");
const axios = require("axios");
let url = "https://res.cloudinary.com/practicaldev/image/fetch/s--nh8zSFgY--/c_fill,f_auto,fl_progressive,h_320,q_auto,w_320/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/285604/94170c66-2590-4002-90e0-ec4dc94ed7b5.png";

async function downloadImage(url){
    try{
    // download the raw image file
    const raw = await axios.get(url, {
        responseType: "arraybuffer",
    })

    // create a base64 encoded string
    let base64 = Buffer.from(raw.data, "binary").toString("base64");

    // create image src
    let image = `data:${raw.headers["content-type"]};base64,${base64}`;

    // actual image 
    let imgFile = `<img src="${image}"/>`;
    }catch(e){
        console.log(e);
    }
}
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay