I'm writing a react native app where I'm supposed to send an array of files to an endpoint.
As per the FormData.append MDN docs:
As with regular form data, you can append multiple values with the same name. ... This technique makes it simpler to process multi-file uploads because the resultant data structure is more conducive to looping.
Hence, my code:
data.append('file', document);
// document is a document picker object
// {"size":...,"name":"...","uri":"...","type":"..."}
On sending the request, I get a 500 response with this error:
Cannot use object of type Illuminate\Http\UploadedFile as array
In plain English, the endpoint was expecting an array but it got a UploadedFile. Isn't FormData supposed to send an array?
What's the error in my code? If you need more code snippets, please, request.
FormDatais not supposed to send an array.FormDatasends whatever you tell it to send. In this case you appear to have calledappendonce, so it sent a single object. If you were to callappendmultiple times with the same name, it would send an array (technically, it would send multiple items with the same name, which most server-side frameworks will interpret as an array of array-like construct). - Heretic Monkeyappendmultiple times with the same name. Since it is not clear where thedocumentobject has multiple elements, that's the best I can say. - Heretic MonkeyarrayName[]) indicating that the value being received is an array? Is this clearer? - Kingdom Coder