()
| 445 | }, |
| 446 | |
| 447 | async upload() { |
| 448 | if (this.selectedFiles.length === 0) { |
| 449 | showStatus('请选择要上传的文件', 'error'); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | const progressSection = document.getElementById(this.getElementId('UploadProgressSection')); |
| 454 | const progressFill = document.getElementById(this.getElementId('ProgressFill')); |
| 455 | const progressText = document.getElementById(this.getElementId('ProgressText')); |
| 456 | |
| 457 | progressSection.classList.remove('hidden'); |
| 458 | |
| 459 | const formData = new FormData(); |
| 460 | this.selectedFiles.forEach(file => formData.append('files', file)); |
| 461 | |
| 462 | if (this.selectedFiles.some(f => f.name.endsWith('.zip'))) { |
| 463 | showStatus('正在上传并解压ZIP文件...', 'info'); |
| 464 | } |
| 465 | |
| 466 | try { |
| 467 | const xhr = new XMLHttpRequest(); |
| 468 | xhr.timeout = 300000; // 5分钟 |
| 469 | |
| 470 | xhr.upload.onprogress = (event) => { |
| 471 | if (event.lengthComputable) { |
| 472 | const percent = (event.loaded / event.total) * 100; |
| 473 | progressFill.style.width = percent + '%'; |
| 474 | progressText.textContent = Math.round(percent) + '%'; |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | xhr.onload = () => { |
| 479 | if (xhr.status === 200) { |
| 480 | try { |
| 481 | const data = JSON.parse(xhr.responseText); |
| 482 | showStatus(`成功上传 ${data.uploaded_count} 个${type === 'antigravity' ? 'Antigravity' : ''}文件`, 'success'); |
| 483 | this.clearFiles(); |
| 484 | progressSection.classList.add('hidden'); |
| 485 | } catch (e) { |
| 486 | showStatus('上传失败: 服务器响应格式错误', 'error'); |
| 487 | } |
| 488 | } else { |
| 489 | try { |
| 490 | const error = JSON.parse(xhr.responseText); |
| 491 | showStatus(`上传失败: ${error.detail || error.error || '未知错误'}`, 'error'); |
| 492 | } catch (e) { |
| 493 | showStatus(`上传失败: HTTP ${xhr.status}`, 'error'); |
| 494 | } |
| 495 | } |
| 496 | }; |
| 497 | |
| 498 | xhr.onerror = () => { |
| 499 | showStatus(`上传失败:连接中断 - 可能原因:文件过多(${this.selectedFiles.length}个)或网络不稳定。建议分批上传。`, 'error'); |
| 500 | progressSection.classList.add('hidden'); |
| 501 | }; |
| 502 | |
| 503 | xhr.ontimeout = () => { |
| 504 | showStatus('上传失败:请求超时 - 文件处理时间过长,请减少文件数量或检查网络连接', 'error'); |
nothing calls this directly
no test coverage detected