我使用下面的代码下载文件并获取其大小 下载文件。它给我0值。但是文件下载得很好。
var download = function(uri, filename, callback){
BASE.request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
BASE.request(uri).pipe(BASE.FS.createWriteStream('images/'+filename)).on('close', callback);
console.log('images/'+filename);
var stats = BASE.FS.statSync('images/'+filename);
var fileSizeInBytes = stats["size"];
console.log(fileSizeInBytes);
//Convert the file size to megabytes (optional)
var fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
console.log(fileSizeInMegabytes);
});
};
download('https://www.google.com/images/srpr/logo3w.png', 'google.png', function(){
console.log('done');
});
答案 0 :(得分:1)
实际文件大小仅在“关闭”后才可用。事件已经发出。你需要等待管道先完成:
BASE.request(uri).pipe(BASE.FS.createWriteStream('images/'+filename)).on('close', function(err) {
console.log('images/'+filename);
var stats = BASE.FS.statSync('images/'+filename);
var fileSizeInBytes = stats["size"];
console.log(fileSizeInBytes);
//Convert the file size to megabytes (optional)
var fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
console.log(fileSizeInMegabytes);
callback(err);
});