[0.2.7.8][ci]
All checks were successful
CI / docker-ci (push) Successful in 26s

This commit is contained in:
DSQ
2026-03-23 11:02:00 +08:00
parent c9611fa622
commit 9e3fe7150b
2 changed files with 105 additions and 98 deletions

View File

@@ -494,6 +494,11 @@ uploadForm.addEventListener('submit', async (e) => {
body: formData,
});
clearInterval(timer);
const ct = (resp.headers.get('content-type') || '').toLowerCase();
if (!ct.includes('application/json')) {
const text = await resp.text();
throw new Error(text ? String(text).slice(0, 200) : `HTTP ${resp.status}`);
}
const data = await resp.json();
if (!resp.ok || data.status !== 'success') {
throw new Error(data.message || '上传识别失败');

View File

@@ -722,6 +722,7 @@ def upload_page(request):
# 上传并识别(不入库)
@require_http_methods(["POST"])
def upload(request):
try:
if request.session.get("user_id") is None:
fallback_uid = request.POST.get("user_id") or request.GET.get("user_id")
if fallback_uid:
@@ -741,11 +742,10 @@ def upload(request):
images_dir = os.path.join(settings.MEDIA_ROOT, "images")
os.makedirs(images_dir, exist_ok=True)
# 按照原始文件进行分组处理
file_results = []
for f in files:
group_images = [] # 存储该文件生成的所有图片路径信息 (abs_path, filename)
group_images = []
is_pdf = f.name.lower().endswith('.pdf')
if is_pdf:
@@ -780,7 +780,6 @@ def upload(request):
dst.write(chunk)
group_images.append((abs_path, filename))
# 对该组图片并行进行 OCR 识别
def run_ocr(img_info):
abs_p, fname = img_info
try:
@@ -797,23 +796,24 @@ def upload(request):
if res:
group_data_list.append(res)
# 合并该文件的多页识别结果
merged_group_data = {}
for item in group_data_list:
if not isinstance(item, dict): continue
if not isinstance(item, dict):
continue
for k, v in item.items():
key = str(k).strip()
if not key: continue
if not key:
continue
if key not in merged_group_data or merged_group_data.get(key) in (None, ''):
merged_group_data[key] = v
elif merged_group_data.get(key) != v:
base = key
idx = 2
while f"{base}_{idx}" in merged_group_data: idx += 1
while f"{base}_{idx}" in merged_group_data:
idx += 1
merged_group_data[f"{base}_{idx}"] = v
if not merged_group_data:
# 如果没识别到,至少保留一个空结构或者包含文件名的提示
merged_group_data = {"文件名": f.name, "提示": "未识别到具体内容"}
rel_paths = [f"images/{img[1]}" for img in group_images]
@@ -831,6 +831,8 @@ def upload(request):
"message": f"成功处理 {len(file_results)} 个文件,请确认数据后点击录入",
"items": file_results,
})
except Exception as e:
return JsonResponse({"status": "error", "message": str(e) or "上传失败"}, status=500)
# 确认并入库