图片识别

This commit is contained in:
李鹏宇
2026-06-12 15:37:48 +08:00
parent f9031b1b97
commit e6c17cd43d
2 changed files with 28 additions and 25 deletions
+14 -12
View File
@@ -95,21 +95,23 @@ export class AiChatAction extends Component {
body: formData,
credentials: "same-origin",
});
// 先读 text,再尝试解析 JSON(避免 body stream already read)
const text = await resp.text();
let data;
try {
data = JSON.parse(text);
} catch (_) {
data = null;
}
if (!resp.ok) {
let detail = "";
try {
const errData = await resp.json();
detail = errData.error || JSON.stringify(errData);
} catch (_) {
const text = await resp.text();
detail = text.substring(0, 500);
}
let detail = (data && data.error) || text.substring(0, 300);
console.error("[AI] Upload HTTP", resp.status, detail);
alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误"));
alert("上传失败 (HTTP " + resp.status + ")\n" + detail);
return;
}
const data = await resp.json();
if (data.success) {
if (data && data.success) {
this.state.attachments = [
...this.state.attachments,
{
@@ -119,7 +121,7 @@ export class AiChatAction extends Component {
},
];
} else {
alert(data.error || "上传失败");
alert((data && data.error) || "上传失败");
}
} catch (e) {
console.error("[AI] Upload failed:", e);
+14 -13
View File
@@ -95,22 +95,23 @@ async function uploadFile(file) {
body: formData,
credentials: "same-origin",
});
// 先读 text,再尝试解析 JSON(避免 body stream already read)
const text = await resp.text();
let data;
try {
data = JSON.parse(text);
} catch (_) {
data = null;
}
if (!resp.ok) {
// 服务器返回错误状态码,尝试读取错误详情
let detail = "";
try {
const errData = await resp.json();
detail = errData.error || JSON.stringify(errData);
} catch (_) {
const text = await resp.text();
detail = text.substring(0, 500);
}
let detail = (data && data.error) || text.substring(0, 300);
console.error("[AI] Upload HTTP", resp.status, detail);
alert("上传失败 (HTTP " + resp.status + ")\n" + (detail || "未知错误"));
alert("上传失败 (HTTP " + resp.status + ")\n" + detail);
return;
}
const data = await resp.json();
if (data.success) {
if (data && data.success) {
pendingAttachments.push({
attachment_id: data.attachment_id,
filename: data.filename,
@@ -119,7 +120,7 @@ async function uploadFile(file) {
});
renderAttachmentPreview();
} else {
alert(data.error || "上传失败");
alert((data && data.error) || "上传失败");
}
} catch (e) {
console.error("[AI] Upload failed:", e);