// 引入请求模块
var request = require('request');
// 此刻拿到了图片的src
let imgSrc = '真实的图片地址';
try {
let opts = {
url: imgSrc
};
var time = new Date().getTime();
let path = "/tmp/"+ time+".png";
await new Promise((resolve, reject) => {
request.get(opts).on('response', response => {
console.log("img type:", response.headers['content-type']);
}).pipe(fs.createWriteStream(path)).on("error", e => {
console.log("pipe error", e);
resolve('');
}).on("finish", () => {
console.log("finish");
resolve("ok");
}).on("close", () => {
console.log("close");
resolve();
});
});
} catch (e) {
console.log(e);
}
网友评论