refactor: 优化oss下载进度提示

This commit is contained in:
dap 2025-01-17 20:35:13 +08:00
parent 0eaa63b2c2
commit c08f9efb1a
3 changed files with 39 additions and 2 deletions

View File

@ -13,7 +13,7 @@
- 字典新增对Number类型的支持 -> `getDictOptions('', true);`即可获取number类型的value
- 文件上传 增加上传进度条 下方上传提示
- 图片上传 增加上传进度条 下方上传提示
- oss下载进度(已下载的KB 无法作为进度显示 total返回为null)
- oss下载进度提示
**BUG FIXES**

View File

@ -0,0 +1,31 @@
/**
*
*
*
* B, KB, MB, GB, TB
*
* @param size
* @param isInteger false如果设置为true
* false
* 3
* @returns "4.5KB""3MB"
*/
export function calculateFileSize(size: number, isInteger = false) {
// 定义文件大小的单位数组
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
// 定义换算基数1KB = 1024B1MB = 1024KB以此类推
const base = 1024;
// 初始化单位索引初始值为0即默认单位为B
let unitIndex = 0;
// 当文件大小大于等于基数且单位索引未超出单位数组范围时,循环进行单位转换
while (size >= base && unitIndex < units.length - 1) {
size /= base;
unitIndex++;
}
// 根据是否需要整数大小,确定输出的精度
const precision = isInteger ? 0 : Math.min(unitIndex, 3);
// 返回格式化后的文件大小字符串
return `${size.toFixed(precision)}${units[unitIndex]}`;
}

View File

@ -30,6 +30,7 @@ import {
} from '#/adapter/vxe-table';
import { configInfoByKey } from '#/api/system/config';
import { ossDownload, ossList, ossRemove } from '#/api/system/oss';
import { calculateFileSize } from '#/utils/file';
import { downloadByData } from '#/utils/file/download';
import { columns, fallbackImageBase64, querySchema } from './data';
@ -112,8 +113,13 @@ async function handleDownload(row: OssFile) {
});
try {
const data = await ossDownload(row.ossId, (e) => {
//
const percent = Math.floor((e.loaded / e.total!) * 100);
downloadSize.value = `已下载: ${Math.floor(e.loaded / 1024)}KB / ${percent}%`;
//
const current = calculateFileSize(e.loaded);
//
const total = calculateFileSize(e.total!);
downloadSize.value = `已下载: ${current}/${total} (${percent}%)`;
});
downloadByData(data, row.originalName);
message.success('下载完成');