无法通过 axios 在 react-native 中将 Image 附加到数组的特定索引和 post with formdata
Can't append Image at specific index of array and post with formdata through axios in react-native
我在 react-native 中使用 formdata 通过 axios 发送图像和内容。
The response I want to send is something like this。
如果数据库中已经存在两张图片,我会在索引 2 处插入图像,将第 0 和第 1 个索引留空。同样,如果我必须更新我的第二张图片,我会上传图片并将其推送到第一个索引。
当我在 postman 上测试我的 api 时,图像被插入到数据库中。 Here is a screenshot of postman response
现在,当我通过 React Native 发送我的表单数据时,它没有显示在数据库中,尽管图像已保存到我的上传文件夹中。这是我的本机代码
if (self.state.imagesArray.length > 0){
var count = self.state.images_key.length;
self.state.imagesArray.forEach((image, i) => {
var file = {
uri : image.uri,
type: image.type,
name: image.name
}
console.log(arr)
formData.append(image[count], file)
count= count + 1;
});}
这里的 imagesArray 是每个上传文件插入索引 0,1 的数组,依此类推。
images_key 是数据库中已经存在的图像。如果计数为 2(例如),我将从 imagesArray 获取图像并将其附加到第二个索引索引处。但是图像永远不会出现在数据库中。当我在 Postman 中执行时,它显示了!
解决了。
self.state.imagesArray.forEach((image, i) => {
var file = {
uri : image.uri,
type: image.type,
name: image.name
}
arr[count] = file;
console.log(arr);
formData.append("image["+count+"]", arr[count])
count= count + 1;
});
我在 react-native 中使用 formdata 通过 axios 发送图像和内容。 The response I want to send is something like this。 如果数据库中已经存在两张图片,我会在索引 2 处插入图像,将第 0 和第 1 个索引留空。同样,如果我必须更新我的第二张图片,我会上传图片并将其推送到第一个索引。
当我在 postman 上测试我的 api 时,图像被插入到数据库中。 Here is a screenshot of postman response
现在,当我通过 React Native 发送我的表单数据时,它没有显示在数据库中,尽管图像已保存到我的上传文件夹中。这是我的本机代码
if (self.state.imagesArray.length > 0){
var count = self.state.images_key.length;
self.state.imagesArray.forEach((image, i) => {
var file = {
uri : image.uri,
type: image.type,
name: image.name
}
console.log(arr)
formData.append(image[count], file)
count= count + 1;
});}
这里的 imagesArray 是每个上传文件插入索引 0,1 的数组,依此类推。 images_key 是数据库中已经存在的图像。如果计数为 2(例如),我将从 imagesArray 获取图像并将其附加到第二个索引索引处。但是图像永远不会出现在数据库中。当我在 Postman 中执行时,它显示了!
解决了。
self.state.imagesArray.forEach((image, i) => {
var file = {
uri : image.uri,
type: image.type,
name: image.name
}
arr[count] = file;
console.log(arr);
formData.append("image["+count+"]", arr[count])
count= count + 1;
});