WACATW commited on
Commit
ccc8f57
·
verified ·
1 Parent(s): 503c740

Update static/js/app.js

Browse files
Files changed (1) hide show
  1. static/js/app.js +26 -32
static/js/app.js CHANGED
@@ -57,7 +57,6 @@ function uploadFiles(files) {
57
  let uploaded = 0;
58
  let failed = 0;
59
 
60
- // 使用 Promise 链式调用,保证按顺序上传并计算每个文件的网速
61
  const uploadNext = (index) => {
62
  if (index >= files.length) {
63
  setTimeout(() => {
@@ -67,7 +66,7 @@ function uploadFiles(files) {
67
  } else {
68
  showToast(`${uploaded} 个文件上传成功!`, 'success');
69
  }
70
- updateStorageInfo(); // 👈 加在这里
71
  refreshFileList();
72
  }, 500);
73
  return;
@@ -96,7 +95,7 @@ function uploadFiles(files) {
96
 
97
  const now = Date.now();
98
  const timeDiff = (now - lastTime) / 1000;
99
- if (timeDiff > 0.5) { // 每0.5秒更新一次网速
100
  const byteDiff = event.loaded - lastLoaded;
101
  const speedBps = byteDiff / timeDiff;
102
  uploadSpeed.textContent = formatSpeed(speedBps);
@@ -107,28 +106,34 @@ function uploadFiles(files) {
107
  };
108
 
109
  xhr.onload = function() {
110
- // 检查 HTTP 状态码和 JSON 里的 success 字段
111
- if (xhr.status === 200) {
112
- try {
113
- const data = JSON.parse(xhr.responseText);
114
- if (data.success) {
115
- uploaded++;
 
 
 
 
 
 
 
116
  } else {
117
  failed++;
118
- console.error('后端上传失败:', data.error);
119
  }
120
- } catch (e) {
121
- failed++;
122
- console.error('解析响应失败:', e);
123
- }
124
- } else {
125
- failed++;
126
- console.error('上传请求失败, 状态码:', xhr.status);
127
- }
128
- uploadNext(index + 1);
129
- };
130
 
131
- // 👇 新增一个网速格式化辅助函数(放在 uploadFiles 函数下方即可)
132
  function formatSpeed(bytesPerSecond) {
133
  if (bytesPerSecond < 1024) return bytesPerSecond.toFixed(1) + ' B/s';
134
  if (bytesPerSecond < 1024 * 1024) return (bytesPerSecond / 1024).toFixed(1) + ' KB/s';
@@ -592,14 +597,3 @@ function updateStorageInfo() {
592
  console.error('Storage info error:', error);
593
  });
594
  }
595
-
596
- // Override uploadFiles to update storage after
597
- const originalUploadFiles = uploadFiles;
598
- uploadFiles = function(files) {
599
- originalUploadFiles(files);
600
- // Update storage after upload completes (polling)
601
- setTimeout(() => {
602
- updateStorageInfo();
603
- }, 2000);
604
- };
605
-
 
57
  let uploaded = 0;
58
  let failed = 0;
59
 
 
60
  const uploadNext = (index) => {
61
  if (index >= files.length) {
62
  setTimeout(() => {
 
66
  } else {
67
  showToast(`${uploaded} 个文件上传成功!`, 'success');
68
  }
69
+ updateStorageInfo(); // 上传完成后更新容量
70
  refreshFileList();
71
  }, 500);
72
  return;
 
95
 
96
  const now = Date.now();
97
  const timeDiff = (now - lastTime) / 1000;
98
+ if (timeDiff > 0.5) {
99
  const byteDiff = event.loaded - lastLoaded;
100
  const speedBps = byteDiff / timeDiff;
101
  uploadSpeed.textContent = formatSpeed(speedBps);
 
106
  };
107
 
108
  xhr.onload = function() {
109
+ if (xhr.status === 200) {
110
+ try {
111
+ const data = JSON.parse(xhr.responseText);
112
+ if (data.success) {
113
+ uploaded++;
114
+ } else {
115
+ failed++;
116
+ console.error('后端上传失败:', data.error);
117
+ }
118
+ } catch (e) {
119
+ failed++;
120
+ console.error('解析响应失败:', e);
121
+ }
122
  } else {
123
  failed++;
124
+ console.error('上传请求失败, 状态码:', xhr.status);
125
  }
126
+ uploadNext(index + 1); // 无论成功失败,继续下一个
127
+ };
128
+
129
+ xhr.open('POST', '/api/upload', true);
130
+ xhr.send(formData);
131
+ }; // 👈 这里闭合 uploadNext 箭头函数
132
+
133
+ uploadNext(0);
134
+ } // 👈 这里闭合 uploadFiles 函数
 
135
 
136
+ // 网速格式化辅助函数
137
  function formatSpeed(bytesPerSecond) {
138
  if (bytesPerSecond < 1024) return bytesPerSecond.toFixed(1) + ' B/s';
139
  if (bytesPerSecond < 1024 * 1024) return (bytesPerSecond / 1024).toFixed(1) + ' KB/s';
 
597
  console.error('Storage info error:', error);
598
  });
599
  }