Browse Source

fix(util): 处理文件大小格式化函数的无效输入

yz 2 weeks ago
parent
commit
42672d5b31
1 changed files with 5 additions and 1 deletions
  1. 5 1
      src/util/util.js

+ 5 - 1
src/util/util.js

@@ -368,10 +368,14 @@ export const downloadFileBase64 = (path, name) => {
 
 /**
  * 格式化文件大小
- * @param {number} bytes - 文件大小(字节)
+ * @param {number|null|undefined} bytes - 文件大小(字节)
  * @returns {string} 格式化后的文件大小
  */
 export const formatFileSize = (bytes) => {
+  // 处理null、undefined、非数字值
+  if (bytes === null || bytes === undefined || isNaN(bytes) || bytes < 0) {
+    return '-/-'
+  }
   if (bytes === 0) return '0 B'
   const k = 1024
   const sizes = ['B', 'KB', 'MB', 'GB', 'TB']