0
点赞
收藏
分享

微信扫一扫

js:filesize.js将文件大小转换为人类可读的格式


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

文档:

  • ​​https://filesizejs.com/​​
  • ​​https://www.npmjs.com/package/filesize​​
  • ​​https://github.com/avoidwork/filesize.js​​

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>



举报

相关推荐

0 条评论