✨ upload image one by one
This commit is contained in:
parent
e8ed31d335
commit
b71b8c1b23
@ -824,35 +824,43 @@
|
|||||||
updateDataStats();
|
updateDataStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 批量上传图片(调用批量API以利用多卡并行)
|
// 批量上传图片(串行:并发=1,每次上传一张,调用 /api/add_image)
|
||||||
async function uploadBatchImages(files) {
|
async function uploadBatchImages(files) {
|
||||||
try {
|
try {
|
||||||
const progressDiv = document.getElementById('imageUploadProgress');
|
const progressDiv = document.getElementById('imageUploadProgress');
|
||||||
const progressBar = progressDiv.querySelector('.progress-bar');
|
const progressBar = progressDiv.querySelector('.progress-bar');
|
||||||
const progressText = document.getElementById('imageProgressText');
|
const progressText = document.getElementById('imageProgressText');
|
||||||
|
|
||||||
progressDiv.style.display = 'block';
|
progressDiv.style.display = 'block';
|
||||||
progressText.textContent = `0/${files.length}`;
|
progressText.textContent = `0/${files.length}`;
|
||||||
progressBar.style.width = '10%';
|
progressBar.style.width = '0%';
|
||||||
|
|
||||||
showAlert('info', `正在批量上传 ${files.length} 张图片...`);
|
showAlert('info', `开始上传 ${files.length} 张图片(串行)...`);
|
||||||
|
|
||||||
const formData = new FormData();
|
let successCount = 0;
|
||||||
for (let i = 0; i < files.length; i++) {
|
for (let i = 0; i < files.length; i++) {
|
||||||
formData.append('images', files[i]);
|
const formData = new FormData();
|
||||||
|
formData.append('image', files[i]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch('/api/add_image', { method: 'POST', body: formData });
|
||||||
|
const data = await resp.json();
|
||||||
|
if (data && data.success) {
|
||||||
|
successCount++;
|
||||||
|
} else {
|
||||||
|
// 不中断流程,记录失败
|
||||||
|
console.warn('Upload failed for file:', files[i].name, data?.error);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Request failed for file:', files[i].name, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
const progress = Math.round(((i + 1) / files.length) * 100);
|
||||||
|
progressBar.style.width = `${progress}%`;
|
||||||
|
progressText.textContent = `${i + 1}/${files.length}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch('/api/add_images_batch', {
|
showAlert('success', `上传完成:成功 ${successCount}/${files.length} 张`);
|
||||||
method: 'POST',
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
const data = await response.json();
|
|
||||||
if (!data.success) {
|
|
||||||
throw new Error(data.error || '批量上传失败');
|
|
||||||
}
|
|
||||||
progressBar.style.width = '100%';
|
|
||||||
progressText.textContent = `${files.length}/${files.length}`;
|
|
||||||
showAlert('success', data.message || `成功上传 ${files.length} 张图片`);
|
|
||||||
await autoSaveIndex();
|
await autoSaveIndex();
|
||||||
updateDataStats();
|
updateDataStats();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user