简体   繁体   English

在 React Native 中上传多张图片时网络请求失败

[英]Network request failed on uploading multiple images in react native

I'm uploading images through fetch api .我正在通过fetch api上传图片。 While uploading, if a single images is selected it works perfectly but if multiple images are selected it gives error [TypeError: Network request failed] .上传时,如果选择了单个图像,则效果很好,但如果选择了多个图像,则会出现错误[TypeError: Network request failed] i have tried commenting initializeFlipper and looked this answer also.我试过评论initializeFlipper查看了这个答案。 Server takes array of images.. this is how images[] looks like when multiple images are selected服务器获取图像数组。这就是选择多个图像时images[]的样子

[{"height": 900, "mime": "image/jpeg", "modificationDate": "1649410153000", "path": "file:///data/user/0/com.emilio/cache/react-native-image-crop-picker/FB_IMG_1649005113656.jpg", "size": 78059, "width": 720}, {"height": 4160, "mime": "image/jpeg", "modificationDate": "1649410153000", "path": "file:///data/user/0/com.emilio/cache/react-native-image-crop-picker/IMG_20220330_134929_1.jpg", "size": 923567, "width": 1920}]

and this is how images looks like when single images is selected这就是选择单个图像时images的样子

[{"height": 725, "mime": "image/jpeg", "modificationDate": "1649410757000", "path": "file:///data/user/0/com.emilio/cache/react-native-image-crop-picker/FB_IMG_1649089342911.jpg", "size": 51443, "width": 720}]

this is my code:这是我的代码:

  const postOrder = () => {
    var data = new FormData();
    data.append('marca', marca);
    data.append('modeo', modeo);
    data.append('option', option);
    data.append('description', description);
    data.append('images[]', {
      uri: images,
      name: 'image.jpg',
      type: 'image/jpeg',
    });
    data.append('userId', state.user.id);
    dispatch(saveOrder(data));
  };

export const saveOrder = data => dispatch => {
  console.log('/data in order action', data);
  fetch(`${baseUrl}/save-order`, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'multipart/form-data',
    },
    body: data,
  })
    .then(res => res.json())
    .then(json => {
      console.log('json1', json);
      alert('order placed');
    })
    .catch(error => {
      console.log('error in saving', error);
    });
};

Edit checked with postman..multiple images are uploading successfully..this is how postman sending it.编辑检查 postman..多张图片上传成功..这就是 postman 发送它的方式。 does anybody know how should i format images[] param有人知道我应该如何格式化 images[] 参数吗邮差

[ TypeError: Network request failed] is thrown when the parameters you send are not correctly formatted. [ TypeError: Network request failed]当您发送的参数格式不正确时抛出。 So try the other way I mentioned below.所以尝试我下面提到的另一种方式。

Try doing a loop and append尝试做一个循环和 append

images.map((file, index) => {
   data.append(`images[${index}]`, {
      uri: file,
      name: 'image.jpg',
      type: 'image/jpeg',
   });
})

Also, ask your backend developer to check if they are accepting the file in the correct param and way.另外,请您的后端开发人员检查他们是否以正确的参数和方式接受文件。 Cross-check the same with the postman to confirm.交叉检查与 postman 相同以确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM