Photo upload on React Native Android produce Type Error Network Error

心不动则不痛 提交于 2020-06-17 10:01:37

问题


I'm executing a photo upload using the fetch API and I keep receiving Type Error Network Request Error. I receive the same error on the emulator and a device. I'm using react-native-image-crop-picker as the source for the photo upload data. Any thoughts?

const handlePhotoUpload =  async (image: any, token: string) => {

      const { path, filename, mime } = image;

      const uri = path.replace("file://", "")
      const file = {
        uri,            
        type: mime,           
        name: filename             
      };

      const body = new FormData()
      body.append('file', file)

      const config = {
        method: 'POST', 
        headers: { 'Authorization': 'Bearer ' + token },
        body
      };

      return await fetch(`${<API URL>}/user/photo`, config)

}

回答1:


If the way you send it is Android, the way is wrong.

The correct uri is:

uri: Platform.OS === "android" ? path : path.replace("file://", "")


来源:https://stackoverflow.com/questions/58814583/photo-upload-on-react-native-android-produce-type-error-network-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!