如何在 Node.js 中获取文件大小
要在 NodeJS 中确定文件大小(例如获取 myfile.txt 的大小),使用 fs.stat() 或 fs.statSync(),如下所示:
get-filesize.js
const fs = require("fs"); //加载文件系统模块
const stats = fs.statSync("myfile.txt");
const fileSizeInBytes = stats.size;
//将文件大小转换为兆字节(可选)
const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;另一个选择是使用以下函数:
getFilesizeInBytes.js
function getFilesizeInBytes(filename) {
const stats = fs.statSync(filename);
const fileSizeInBytes = stats.size;
return fileSizeInBytes;
}Check out similar posts by category:
NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow