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