nodejs使用superangent下载图像,检查文件大小

时间:2016-07-18 03:06:40

标签: node.js download fs superagent

...
superagent
            .get(req.body.img)
            .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36')
            .pipe(fs.createWriteStream(filePath));
console.log(fs.statSync(filePath));
...

输出是     {"设备":2049,"模式":33204" NLINK":1," UID":1000," GID& #34;:1000," RDEV":0," BLKSIZE":4096,"鲍":6578368,"大小":0, "块":0,"的atime":" 2016-07-18T02:46:12.845Z""修改时间":&#34 ; 2016-07-18T02:46:12.845Z""的ctime":" 2016-07-18T02:46:12.845Z"" birthtime" :" 2016-07-18T02:46:12.845Z"}

为什么文件大小为0,我检查文件大小:

console.log(fs.statSync(filePath));

{dev:2049,   模式:33204,   nlink:1,   uid:1000,   gid:1000,   rdev:0,   blksize:4096,   ino:1975657,   大小:1964074,   街区:3840,   atime:2016-07-18T02:29:16.977Z,   mtime:2016-07-18T02:28:30.037Z,   ctime:2016-07-18T02:28:30.037Z,   出生时间:2016-07-18T02:28:30.037Z}

1 个答案:

答案 0 :(得分:1)

也许,您在流写入文件时调用console.log。尝试在finish上添加侦听器。

var stream = fs.createWriteStream(filePath);
stream.on('finish', function () {
    console.log(fs.statSync(filePath)); 
});

superagent 
    ...
    .pipe(stream);