简体   繁体   English

React原生图片上传getting.network错误

[英]React native image upload getting network error

'react-native-image-picker' for uploading image in my application, Sometimes it is uploading and sometimes i am getting [TypeError: Network request failed] below is the code: 'react-native-image-picker'用于在我的应用程序中上传图片,有时它正在上传,有时我得到[TypeError: Network request failed]下面是代码:

FormData in my component:我的组件中的 FormData:

//image is :file:///data/user/0/com.testApp/cache/rn_image_picker_lib_temp_0d38d959-6ece-4750-a215-4b3f68002f4e.jpg
let formData = new FormData();
  formData.append('images', { uri:image, name: imageSelected?.fileName, type: 'image/png' });
const response =  await updateUserProfile(userDetails,formData)

Service call:服务电话:

 export const updateUserProfile = async (userDetails,data) => {
    const response = await fetch(`${baseUrl}/updateusersprofile/${userDetails._id}`, {
        method: "PATCH",
        headers: {
          //"Content-Type": "application/json",
          'Content-Type': 'multipart/form-data',
          Authorization: `Bearer ${userDetails.token}`,
        },
        body: data,
      });
    return await response;
  };

In Postman i have checked the api is working fine, What would be the problem in my code.在 Postman 中,我检查了 api 工作正常,我的代码会出现什么问题。

export const updateUserProfile = async (userDetails,data) => { const response = await fetch( ${baseUrl}/updateusersprofile/${userDetails._id} , { method: "PATCH", headers: { //"Content-Type": "application/json", 'Content-Type': 'multipart/form-data', Authorization: Bearer ${userDetails.token} , }, body: JSON.stringify(data), }); export const updateUserProfile = async (userDetails,data) => { const response = await fetch( ${baseUrl}/updateusersprofile/${userDetails._id} , { method: "PATCH", headers: { //"Content-Type" : "application/json", 'Content-Type': 'multipart/form-data', Authorization: Bearer ${userDetails.token} , }, body: JSON.stringify(data), }); return await response;返回等待响应; }; };

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

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