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

时间:2021-12-01 09:00:51
...
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));
...

The output is {"dev":2049,"mode":33204,"nlink":1,"uid":1000,"gid":1000,"rdev":0,"blksize":4096,"ino":6578368,"size":0,"blocks":0,"atime":"2016-07-18T02:46:12.845Z","mtime":"2016-07-18T02:46:12.845Z","ctime":"2016-07-18T02:46:12.845Z","birthtime":"2016-07-18T02:46:12.845Z"}

输出为{“dev”:2049,“mode”:33204,“nlink”:1,“uid”:1000,“gid”:1000,“rdev”:0,“blksize”:4096,“ino”: 6578368, “尺寸”:0, “块”:0 “的atime”: “2016-07-18T02:46:12.845Z”, “修改时间”: “2016-07-18T02:46:12.845Z”,“的ctime “:” 2016-07-18T02:46:12.845Z”, “birthtime”: “2016-07-18T02:46:12.845Z”}

why the file size is 0, i have check the file size with :

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

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

{ dev: 2049, mode: 33204, nlink: 1, uid: 1000, gid: 1000, rdev: 0, blksize: 4096, ino: 1975657, size: 1964074, blocks: 3840, atime: 2016-07-18T02:29:16.977Z, mtime: 2016-07-18T02:28:30.037Z, ctime: 2016-07-18T02:28:30.037Z, birthtime: 2016-07-18T02:28:30.037Z }

{dev:2049,mode:33204,nlink:1,uid:1000,gid:1000,rdev:0,blksize:4096,ino:1975657,size:1964074,blocks: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 个解决方案

#1


1  

Perhaps, you call console.log while stream write to file. Try add listener on finish.

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

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

superagent 
    ...
    .pipe(stream);

#1


1  

Perhaps, you call console.log while stream write to file. Try add listener on finish.

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

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

superagent 
    ...
    .pipe(stream);