yxrj 发表于 2020-3-22 09:52:00

JS格式化文件大小 单位:Bytes、KB、MB、GB

<h4 style="margin-top: 10px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif;">方法一:bytes自适应转换到KB,MB,GB</h4><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 1</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/ &lt;summary&gt;</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 2</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/ 格式化文件大小的JS方法</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 3</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/ &lt;/summary&gt;</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 4</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/ &lt;param name="filesize"&gt;文件的大小,传入的是一个bytes为单位的参数&lt;/param&gt;</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 5</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/ &lt;returns&gt;格式化后的值&lt;/returns&gt;</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 6</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> renderSize(value){<br></span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 7</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">null</span>==value||value==''<span style="line-height: 1.5 !important;">){
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 8</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> "0 Bytes"<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 9</span> <span style="line-height: 1.5 !important;">    }
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">10</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> unitArr = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span> Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">11</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> index=0<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">12</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> srcsize =<span style="line-height: 1.5 !important;"> parseFloat(value);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">13</span>   index=Math.floor(Math.log(srcsize)/Math.log(1024));
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">14</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> size =srcsize/Math.pow(1024,index);
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">15</span>   size=size.toFixed(2);<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">保留的小数位数</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">16</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> size+<span style="line-height: 1.5 !important;">unitArr;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">17</span> }</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif;">&nbsp;</p><a name="_label0_1" style="color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif;"></a><h4 style="margin-top: 10px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif;">  方法二:bytes自适应转换到KB,MB,GB</h4><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 1</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> formatFileSize(fileSize) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 2</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (fileSize &lt; 1024<span style="line-height: 1.5 !important;">) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 3</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> fileSize + 'B'<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 4</span>   } <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (fileSize &lt; (1024*1024<span style="line-height: 1.5 !important;">)) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 5</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> temp = fileSize / 1024<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 6</span>         temp = temp.toFixed(2<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 7</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> temp + 'KB'<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 8</span>   } <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (fileSize &lt; (1024*1024*1024<span style="line-height: 1.5 !important;">)) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 9</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> temp = fileSize / (1024*1024<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">10</span>         temp = temp.toFixed(2<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">11</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> temp + 'MB'<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">12</span>   } <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">13</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> temp = fileSize / (1024*1024*1024<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">14</span>         temp = temp.toFixed(2<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">15</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> temp + 'GB'<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">16</span> <span style="line-height: 1.5 !important;">    }
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">17</span> }</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div></div><a name="_label0_2" style="color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif;"></a><h4 style="margin-top: 10px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif;">  方法三:可以设定输入的文件长度的参数的原始单位,自适应转换到KB,MB,GB</h4><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 1</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 2</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> *
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 3</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> * @param{} total [文件大小]
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 4</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> * @param{} n   
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 5</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> * @return {}       [带单位的文件大小的字符串]
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 6</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 7</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> fileLengthFormat(total, n) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 8</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span><span style="line-height: 1.5 !important;"> format;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 9</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> len = total / (1024.0<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">10</span>   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (len &gt; 1000<span style="line-height: 1.5 !important;">) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">11</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> arguments.callee(len, ++<span style="line-height: 1.5 !important;">n);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">12</span>   } <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">13</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">switch</span><span style="line-height: 1.5 !important;"> (n) {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">14</span>             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> 1<span style="line-height: 1.5 !important;">:
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">15</span>               format = len.toFixed(2) + "KB"<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">16</span>               <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span><span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">17</span>             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> 2<span style="line-height: 1.5 !important;">:
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">18</span>               format = len.toFixed(2) + "MB"<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">19</span>               <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span><span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">20</span>             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> 3<span style="line-height: 1.5 !important;">:
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">21</span>               format = len.toFixed(2) + "GB"<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">22</span>               <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span><span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">23</span>             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> 4<span style="line-height: 1.5 !important;">:
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">24</span>               format = len.toFixed(2) + "TB"<span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">25</span>               <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span><span style="line-height: 1.5 !important;">;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">26</span> <span style="line-height: 1.5 !important;">      }
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">27</span>         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="line-height: 1.5 !important;"> format;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">28</span> <span style="line-height: 1.5 !important;">    }
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">29</span> }</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 820px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif;">&nbsp;</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">1</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">假如文件大小为1024byte,想自适应到kb,则如下传参</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">2</span> fileLengthFormat(1024,1);<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">"1.00KB"</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">3</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">假如文件大小为1024kb,想自适应到mb,则如下传参</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">4</span> fileLengthFormat(1024,2);<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">"1.00MB"</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">5</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">测试</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">6</span> fileLengthFormat(112233445566,1);<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">"104.53GB"</span></pre></div><p style="margin: 10px auto; color: rgb(0, 0, 0); font-family: Verdana, Arial, Helvetica, sans-serif;">&nbsp;</p><p></p>
页: [1]
查看完整版本: JS格式化文件大小 单位:Bytes、KB、MB、GB