Commit 0fc799e4 authored by Lê Bảo Hồng Đức's avatar Lê Bảo Hồng Đức

fix: Jodit uploader field name mismatch causing MulterError

Jodit sends files as files[0], files[1], ... (from filesVariableName(i)
returning `files[${i}]`), not files[] as previously assumed. The buildData
callback only matched "files[]" so the actual file field was passed
through as-is, triggering MulterError: Unexpected field because backend
multer.single("file") only accepts a field named "file".

Fix buildData to:
- Match files[0], files[1], ... pattern and convert first file to "file"
- Drop extra files (multer.single only accepts one)
- Drop Jodit-internal fields (source, extension, mimetype) backend ignores

Generated with [Devin](https://devin.ai)
Co-Authored-By: 's avatarDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent 5f187951
...@@ -333,12 +333,31 @@ export function AdminRichTextEditor({ ...@@ -333,12 +333,31 @@ export function AdminRichTextEditor({
format: "json", format: "json",
buildData: (data: FormData) => { buildData: (data: FormData) => {
const next = new FormData(); const next = new FormData();
let firstFileAppended = false;
data.forEach((value, key) => { data.forEach((value, key) => {
// Jodit sends files as files[0], files[1], ... — multer expects
// a single field named "file". Only keep the first file and drop
// the rest (backend uses multer.single("file")).
if (/^files\[\d+\]$/.test(key) && value instanceof File) {
if (!firstFileAppended) {
next.append("file", value);
firstFileAppended = true;
}
return;
}
// Also handle legacy "files[]" pattern just in case.
if (key === "files[]" && value instanceof File) { if (key === "files[]" && value instanceof File) {
next.append("file", value); if (!firstFileAppended) {
} else { next.append("file", value);
next.append(key, value); firstFileAppended = true;
}
return;
}
// Drop Jodit-internal fields the backend doesn't use.
if (key === "source" || key === "extension" || key === "mimetype") {
return;
} }
next.append(key, value);
}); });
return next; return next;
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment