我需要它以 mb 為單位將檔案夾的大小列印到快速網路應用程式
我已經在 kb 中嘗試過了,但我不知道如何讓它以 MB 為單位作業
我試過這個
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:
uj5u.com熱心網友回復:
你會這樣做
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);
在 express 中,你會像這樣列印它。
res.send(fileSizeInMegabytes);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/378995.html
標籤:节点.js
