Topic: Only the first selected attachment reaches our upload handler

itsolutions free asked 2 weeks ago


Our document form allows selecting several files. The change handler receives only one attachment even when multiple selection is enabled.

Which component option controls the full file list?


Mateusz Lazaru staff answered 2 weeks ago


The selection is reaching your handler correctly. setAttachments([files[0]]) is where the other two files are discarded. Copy the complete callback array into state; your existing FormData loop will then append each attachment.

Submit after the selection has appeared in state, rather than calling buildPayload immediately after setAttachments. That setter schedules an update, so an immediate read can still contain the previous selection.

function onFilesChanged(files) {
  setAttachments([...files]);
}
function buildPayload() {
  const payload = new FormData();
  attachments.forEach(file => payload.append('files', file));
  return payload;
}

Mateusz Lazaru staff answered 2 weeks ago


For MDBFileUpload, enable the boolean multiple prop on the upload component itself. Its documented change callback receives a File[] array, so keep that array intact; a wrapper that reads only files[0] will discard the remaining attachments.

Please share the component import, upload JSX, and callback, and log the callback argument before any transformation. If it already contains only one file, a minimal reproduction with two small documents will let us isolate component behavior from your upload handler.

https://mdbootstrap.com/docs/react/plugins/file-upload/


itsolutions free commented 2 weeks ago

The component callback actually logs three files. Our adapter then reduces that list before building the request. This is the relevant part; the server expects every attachment under the files field.

const [attachments, setAttachments] = useState([]);
function onFilesChanged(files) {
  console.log('From component:', files.length);
  setAttachments([files[0]]);
}
function buildPayload() {
  const payload = new FormData();
  attachments.forEach(file => payload.append('files', file));
  return payload;
}


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Desktop
  • Browser: Firefox
  • OS: Windows 11
  • Provided sample code: No
  • Provided link: No