nodejs의 URL에서 이미지를 다운로드하는 방법
3830 단어 javascriptwebdevreactnode
원래 게시물
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);
}
}
Reference
이 문제에 관하여(nodejs의 URL에서 이미지를 다운로드하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ats1999/how-to-download-an-image-from-url-in-nodejs-4730텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)