FormData 的不明确行为:将文件数组发送到 API

Unclear Behaviour of FormData: Sending an Array of Files to API

我正在编写一个本机反应应用程序,我应该在其中将一组文件发送到端点。

根据 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.

因此,我的代码:

data.append('file', document);
// document is a document picker object
// {"size":...,"name":"...","uri":"...","type":"..."}

发送请求时,我收到 500 响应并出现以下错误:

Cannot use object of type Illuminate\Http\UploadedFile as array

用简单的英语来说,端点需要一个数组,但它得到了一个 UploadedFile。 FormData 不是应该发送数组吗?

我的代码有什么错误? 如果您需要更多代码片段,请索取。

从错误信息来看,你的服务器端技术好像是PHP。如果是这样,你可以告诉 PHP file 是一个数组 by adding [] after it,例如:

data.append('file[]', document);
// −−−−−−−−−−−−−−^^