filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.

文档:

NPM

安装

npm i -S filesize

使用示例

const filesize = require('filesize');

console.log(filesize(1024)); // 1.02 kB

console.log(filesize(1024 * 1024)); // 1.05 MB

console.log(filesize(1024 * 1024 * 1024)); // 1.07 GB

console.log(filesize(1024 * 1024 * 1024 * 1024)); // 1.1 TB

CDN

# 生产环境
https://cdnjs.cloudflare.com/ajax/libs/filesize/8.0.7/filesize.min.js

# 开发环境
https://cdnjs.cloudflare.com/ajax/libs/filesize/8.0.7/filesize.js

使用示例

<script src="https://cdnjs.cloudflare.com/ajax/libs/filesize/8.0.7/filesize.min.js"></script>

<script>
console.log(filesize(1024)); // 1.02 kB

console.log(filesize(1024 * 1024)); // 1.05 MB

console.log(filesize(1024 * 1024 * 1024)); // 1.07 GB

console.log(filesize(1024 * 1024 * 1024 * 1024)); // 1.1 TB
</script>