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

deekepMaks's avatar

how to add object containing file in form date

I have an object like this:

let obj = {
		something: 'something',
		file: {} //this contains the File
}

then I add this object to the formdata formData.append('obj', obj)

But on the server I get the [object object]

I understand that I can just send the file separately not in the object, but I would like to send it in my version. Is it possible?

0 likes
1 reply
s3r0s4pi3ns's avatar
Level 2

Hi, the file needs to be in Blob format as it's defined in the specification. I'll recommend you to separate the object and file in different keys inside the FormData. Anyways to avoid the [object, object] it's because you need to serialize the object before append to the form data like this:

formData.append('my-object', JSON.stringify(obj));

https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

Please or to participate in this conversation.