Converting file size in bytes to human-readable string

后端 未结 19 2147
眼角桃花
眼角桃花 2020-11-28 17:58

I\'m using this function to convert a file size in bytes to a human-readable file size:

function getReadableFileSizeString(fileSizeInBytes) {
    var i = -1;         


        
19条回答
  •  情书的邮戳
    2020-11-28 18:25

    For those who use Angular, there's a package called angular-pipes that has a pipe for this:

    File

    import { BytesPipe } from 'angular-pipes';
    

    Usage

    {{ 150 | bytes }} 
    {{ 1024 | bytes }} 
    {{ 1048576 | bytes }} 
    {{ 1024 | bytes: 0 : 'KB' }} 
    {{ 1073741824 | bytes }} 
    {{ 1099511627776 | bytes }} 
    {{ 1073741824 | bytes : 0 : 'B' : 'MB' }} 
    

    Link to the docs.

提交回复
热议问题