diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs index 77b57270c..259aaaea9 100644 --- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs +++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs @@ -916,12 +916,29 @@ namespace IRaCIS.Core.API.Controllers var notifyInterval = TimeSpan.FromSeconds(1); var lastNotify = DateTime.UtcNow; + // 用于计算下载速度 + long lastReceivedSize = 0; + DateTime lastSpeedCheck = DateTime.UtcNow; async Task NotifyProgressAsync(bool force = false) { - if (!force && DateTime.UtcNow - lastNotify < notifyInterval) + + var now = DateTime.UtcNow; + var elapsedSeconds = (now - lastSpeedCheck).TotalSeconds; + + // 如果没有强制推送,并且未到推送间隔,则返回 + if (!force && elapsedSeconds < notifyInterval.TotalSeconds) return; + // 计算下载速度(字节/秒) + double speedBps = 0; + if (elapsedSeconds > 0) + { + speedBps = (receivedSize - lastReceivedSize) / elapsedSeconds; + } + + lastSpeedCheck = now; + lastReceivedSize = receivedSize; lastNotify = DateTime.UtcNow; var progress = new @@ -932,7 +949,11 @@ namespace IRaCIS.Core.API.Controllers SizePercent = totalSize > 0 ? Math.Round(receivedSize * 100m / totalSize, 2).ToString() + "%" - : "0%" + : "0%", + + Speed = (speedBps / 1024 >= 1024 + ? (speedBps / 1024 / 1024).ToString("0.00") + " MB/s" + : (speedBps / 1024).ToString("0.00") + " KB/s") }; // 不阻塞下载流程