nodejs

Download PNg/gif from array of link to our system

router.get("/get", () => {
  // Replace 'apiUrl' with the actual API URL that provides the image or GIF links.
  var imageData = [
    "https://inscribenow.io/uploads/aefd18513509595d.gif",
    "https://inscribenow.io/uploads/40f6be9683c554c5.png",
  ];

  const apiUrl = "http://127.0.0.1:5500/uploads/377a629140a94ac3b221..png";

  async function downloadAndSaveImages() {
    try {
      // const response = await axios.get(apiUrl);
      const images = imageData; // Assuming the API returns an array of image URLs.

      // Create a folder to save the images (you can change 'images_folder' to your desired folder name).
      const folderPath = "./images_folder";
      if (!fs.existsSync(folderPath)) {
        fs.mkdirSync(folderPath);
      }

      for (const imageUrl of images) {
        const filename = imageUrl.split("/").pop(); // Extract the file name from the URL.
        const filePath = `${folderPath}/${filename}`;

        const imageResponse = await axios.get(imageUrl, {
          responseType: "arraybuffer"
        });
        fs.writeFileSync(filePath, imageResponse.data, "binary");
        console.log(`Image saved: ${filePath}`);
      }

      console.log("All images saved successfully!");
    } catch (error) {
      console.error("Error saving images:", error.message);
    }
  }

  downloadAndSaveImages();
});
Output
Images will be saved in our system
Was this helpful?