如何在 Node.js 中找到文件的大小?

我正在使用 multer 上传我的图片和文档,但是这次我想限制上传,如果图片的大小 > 2mb。如何找到文档的文件大小?到目前为止,我尝试如下,但不工作。

var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, common.upload.student);
},
filename: function (req, file, callback) {
console.log(file.size+'!!!!!!!!!!!!!!')======>'Undefined'
var ext = '';
var name = '';
if (file.originalname) {
var p = file.originalname.lastIndexOf('.');
ext = file.originalname.substring(p + 1);
var firstName = file.originalname.substring(0, p + 1);
name = Date.now() + '_' + firstName;
name += ext;
}
var filename = file.originalname;
uploadImage.push({ 'name': name });
callback(null, name);
}
});

Can anyone please help me?

138474 次浏览

要获取以兆字节为单位的文件大小:

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

或以字节为单位:

function getFilesizeInBytes(filename) {
var stats = fs.statSync(filename);
var fileSizeInBytes = stats.size;
return fileSizeInBytes;
}

In addition, you can use the NPM filesize package: https://www.npmjs.com/package/filesize

这个软件包使事情变得更加可配置。

var fs = require("fs"); //Load the filesystem module


var filesize = require("filesize");


var stats = fs.statSync("myfile.txt")


var fileSizeInMb = filesize(stats.size, {round: 0});

更多例子:
Https://www.npmjs.com/package/filesize#examples

The link by @gerryamurphy is broken for me, so I will link to a package I made for this.

Https://github.com/dawsbot/file-bytes

The API is simple and should be easily usable:

fileBytes('README.md').then(size => {
console.log(`README.md is ${size} bytes`);
});

您还可以从 npm: https://www.npmjs.com/package/file-sizeof检查这个包

The API is quite simple, and it gives you the file size in SI and IEC notation.

const { sizeof } = require("file-sizeof");


const si = sizeof.SI("./testfile_large.mp4");
const iec = sizeof.IEC("./testfile_large.mp4");

And the resulting object represents the size from B(byte) up to PB (petabyte).

interface ISizeOf {
B: number;
KB: number;
MB: number;
GB: number;
TB: number;
PB: number;
}

注意: 下面是在电子应用程序的上下文中。

使用以下方法:

window.require("fs")

而不仅仅是:

require("fs")

帮我解决了这个问题。

如果使用 ES6并进行解构,那么以字节为单位查找文件大小只需要2行代码(如果已声明 fs 模块,则需要1行代码):

const fs = require('fs');
const {size} = fs.statSync('path/to/file');

注意,如果已经声明了 size 变量,则此操作将失败。这可以通过重命名变量,同时使用冒号解构来避免:

const fs = require('fs');
const {size: file1Size} = fs.statSync('path/to/file1');
const {size: file2Size} = fs.statSync('path/to/file2');

For anyone looking for a current answer with native packages, here's how to get mb size of a file without blocking the event loop using fs (specifically, 我保证) and async/await:

const fs = require('fs').promises;
const BYTES_PER_MB = 1024 ** 2;


// paste following snippet inside of respective `async` function
const fileStats = await fs.stat('/path/to/file');
const fileSizeInMb = fileStats.size / BYTES_PER_MB;

You can find the size in bytes.

const libFS       = require('fs');
let yourFilesize  = fs.statSync("File path").size
console.log(yourFilesize)