Fix NaN% progress display issue

- Fix undefined totalProgress variable causing NaN% display
- Add defensive programming to handle invalid numeric values
- Ensure progress values are clamped between 0-100
- Add NaN checks in updateProgress and createUploadItem methods
- Prevent progress calculation errors in chunked uploads
This commit is contained in:
admin
2026-01-23 16:00:58 +08:00
parent 5120ed8af0
commit cb88012a6d

View File

@@ -137,7 +137,7 @@
id: file._dzFileId,
fileName: file.name,
totalSize: file.size,
progress: file._uploadProgress || 0,
progress: isNaN(file._uploadProgress) ? 0 : Math.max(0, Math.min(100, file._uploadProgress || 0)),
status: 'uploading',
chunks: null,
startTime: Date.now()
@@ -155,7 +155,8 @@
updateProgress(fileId, progress, chunkInfo) {
const task = this.activeUploads.get(fileId);
if (task) {
task.progress = progress;
// 确保progress是有效的数值
task.progress = isNaN(progress) ? 0 : Math.max(0, Math.min(100, progress));
task.chunks = chunkInfo;
this.notifyUIUpdate();
}
@@ -240,16 +241,19 @@
const statusIcon = task.status === 'completed' ? '✓' :
task.status === 'failed' ? '✗' : '⏳';
// 确保progress是有效的数值
const safeProgress = isNaN(task.progress) ? 0 : Math.max(0, Math.min(100, task.progress));
item.innerHTML = `
<div class="item-icon">${getFileIcon(task.fileName)}</div>
<div class="item-info">
<div class="item-name">${escapeHtml(task.fileName)}</div>
<div class="item-progress">
<div class="progress-bar">
<div class="progress-fill" style="width: ${task.progress}%"></div>
<div class="progress-fill" style="width: ${safeProgress}%"></div>
</div>
<div class="progress-text">
<span class="progress-percent">${Math.round(task.progress)}%</span>
<span class="progress-percent">${Math.round(safeProgress)}%</span>
${task.chunks ? `<span class="progress-chunks">(${task.chunks})</span>` : ''}
</div>
</div>
@@ -568,6 +572,7 @@
this.on('uploadprogress', function(file, progress, bytesSent, totalBytesSent, totalBytes) {
var fileId = file._dzFileId;
var totalProgress = progress; // 默认值
// 更新悬浮窗中的进度
var progressEl = document.getElementById('progress-' + fileId);
@@ -577,7 +582,7 @@
if (file.totalChunks > 1) {
var currentChunk = file.upload.chunk || 0;
var chunkWeight = 100 / file.totalChunks;
var totalProgress = (currentChunk * chunkWeight) + (progress * chunkWeight / 100);
totalProgress = (currentChunk * chunkWeight) + (progress * chunkWeight / 100);
if (progressEl) {
progressEl.style.width = Math.min(totalProgress, 100) + '%';
@@ -589,6 +594,7 @@
chunksEl.textContent = (currentChunk + 1) + '/' + file.totalChunks;
}
} else {
totalProgress = progress;
if (progressEl) {
progressEl.style.width = progress + '%';
}