TypeError:网络请求失败.如何正确地将数组中的多个图像附加到formData.反应自然



我是一个反应原生的新手。我正在尝试将数组中的多个图像附加到formData。像这个

formData.append('application_copy_file', 
[{
uri: this.state.ApplicationCopy,
name: 'upload_application_copy.jpg',
type: 'image/*'
},
{
uri: this.state.ApplicationCopy1,
name: 'upload_application_copy1.jpg',
type: 'image/*'
},
{
uri: this.state.ApplicationCopy2,
name: 'upload_application_copy2.jpg',
type: 'image/*'
},
]);

但是当我这样附加的时候。当我提交表单时,我收到了这样的错误=[TypeError:网络请求失败]

当我只上传一个这样的=

formData.append('application_copy_file', 
{
uri: this.state.ApplicationCopy,
name: 'upload_application_copy.jpg',
type: 'image/*'
}
);

则其工作正常,但表示=count((:参数必须是一个数组或实现Countable的对象。来自服务器response.text。请帮忙。

像这样尝试

formData.append('application_copy_file[0]', 
{
uri: this.state.ApplicationCopy,
name: 'upload_application_copy.jpg',
type: 'image/*'
}
);
formData.append('application_copy_file[1]', 
{
uri: this.state.ApplicationCopy1,
name: 'upload_application_copy.jpg',
type: 'image/*'
}
);

如果我假设你所在州的每一件事都只是为了上传。

你可以,

Object.keys(this.state).forEach((item, i) => {
formData.append(`application_copy_file[${i}]`, 
{
uri: this.state[item],
name: this.state[item] + '.jpg',
type: 'image/*'
}
);
})

您必须传递图像数组,以便将数组放入JSON.stringify(your images array)

最新更新