diff --git a/web/src/lib/util/file_util.ts b/web/src/lib/util/file_util.ts index 5b267350..a7e564ad 100644 --- a/web/src/lib/util/file_util.ts +++ b/web/src/lib/util/file_util.ts @@ -7,7 +7,7 @@ export function getFileURL(record: { [key: string]: any; }, filename?: string, t return filename; } - return `/api/v1/files/${record.collectionId}/${record.id}/${filename}${thumb ? '?thumb='+thumb : ''}` + return `/api/v1/files/${record.collectionId}/${record.id}/${filename}${thumb ? '?thumb=' + thumb : ''}` } export function isURL(value: string) { @@ -50,16 +50,55 @@ export function saveAs(data: Blob, fileName: string) { window.URL.revokeObjectURL(url); }; -function buildFormData(formData: FormData, data: any, parentKey?: string, exclude?: string[]) { - if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File) && !(data instanceof Blob)) { - Object.keys(data).forEach(key => { - if (exclude?.includes(key)) { +function buildFormData( + formData: FormData, + data: any, + parentKey?: string, + exclude: string[] = [] +) { + if (data === null || data === undefined) { + return; + } + + // Handle arrays + if (Array.isArray(data)) { + if (data.length === 0) { + // Add an explicit empty entry for empty arrays + formData.append(parentKey!, ''); + return; + } + + data.forEach((value, index) => { + const key = parentKey ? `${parentKey}` : String(index); + buildFormData(formData, value, key, exclude); + }); + } + + // Handle objects (but not Date, File, Blob) + else if ( + typeof data === 'object' && + !(data instanceof Date) && + !(data instanceof File) && + !(data instanceof Blob) + ) { + Object.keys(data).forEach((key) => { + if (exclude.includes(key)) { return; } - buildFormData(formData, data[key], parentKey ? `${parentKey}` : key); + const value = data[key]; + const newKey = parentKey ? `${parentKey}.${key}` : key; + buildFormData(formData, value, newKey, exclude); }); - } else { - const value = data == null ? '' : data; + } + + // Handle primitives and special types + else { + const value = + data instanceof Date + ? data.toISOString() + : data == null + ? '' + : data; formData.append(parentKey!, value); }