Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ipacs13's avatar

Append Array of Objects to FormData

Hi mates,

I have this array of objects below:

data: [
  {file: File, fileTitle: "file1", description: "desc1"},
  {file: File, fileTitle: "file2", description: "desc2"},
  {file: File, fileTitle: "file3", description: "desc3"},
]

And I am trying to append this array to a FormData before I send a request to an API. However, I cannot figure out how do I append array objects. So far here's my code (processing)

let formData = new FormData();

data.forEach(element => {
   formData.append() // what should I put here?
})

Your responses and help are much appreciated. Thank you so much in advance! :)

0 likes
2 replies
Commander's avatar
let formData = new FormData();

data.forEach(element => {
   formData.append("files[]", element.file, element.fileTitle) // check this out for more info: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append#example
})

Please or to participate in this conversation.