Node.js按顺序下载图片代码
访问量:
591
title: Node.js按顺序下载图片代码
const fs = require("fs");
const path = require("path");
const request = require("request"); // npm install request
// 使用fs下载
async function fs_download_img(img_url) {
const img_name = img_url.split("/").pop();
const local_img_path_name = path.join(__dirname, img_name)
return new Promise((resolve, reject) => {
let ws = fs.createWriteStream(local_img_path_name)
request(img_url).pipe(ws);
ws.on("close", ()=>{
resolve(local_img_path_name);
})
})
}
async function main() {
const img_url_list = [
"http://www.v2fy.com/wp-content/uploads/2020/05/keycat1000.jpg",
"http://www.v2fy.com/wp-content/uploads/2020/12/v2fy-logo-1.png"
];
for(let i = 0, img_url_list_length = img_url_list.length; i<img_url_list_length; i++){
let local_img_path = await fs_download_img(img_url_list[i]);
console.log(local_img_path);
}
}
main()
本文永久更新地址(欢迎来读留言,写评论):
https://www.v2fy.com/p/2021-01-03-nodejs-http-1609650016000
发表回复