获取以兆字节为单位的文件夹大小,然后将其打印到expressweb应用程序(nodejs)中



我需要它将mb中的文件夹大小打印到express web应用程序

我已经在kb中尝试过了,但我不知道如何使它在兆字节中工作

我试过这个

const fs = require('fs');
// Read file stats
fs.stat('file.txt', (err, stats) => {
if (err) {
console.log(`File doesn't exist.`);
} else {
console.log(stats);
}
});

它只是打印这个

Stats {
dev: 16777221,
mode: 33279,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 5172976,
size: 290,
blocks: 8,
atimeMs: 1606143471354.2327,
mtimeMs: 1599218860000,
ctimeMs: 1606074010041.4927,
birthtimeMs: 1605423684000,
atime: 2020-11-23T14:57:51.354Z,
mtime: 2020-09-04T11:27:40.000Z,
ctime: 2020-11-22T19:40:10.041Z,
birthtime: 2020-11-15T07:

你会像这个一样做

var fs = require("fs"); // Load the filesystem module
var stats = fs.statSync("myfile.txt") //load the file 
var fileSizeInBytes = stats.size; //get file size
// Convert the file size to megabytes 
var fileSizeInMegabytes = fileSizeInBytes / (1024*1024);

你只需要像这样打印。

console.log(fileSizeInMegabytes);

在快递中,你会这样打印。

res.send(fileSizeInMegabytes);

最新更新