简体   繁体   English

图像上传与 axios 反应原生 android

[英]Image upload with axios react native android

I am trying to upload image to an API the iOS version is working good but there is problem in android, My code is bellow.我正在尝试将图像上传到 API iOS 版本运行良好,但 android 存在问题,我的代码如下。

const uploadUri =
    Platform.OS === 'ios'
      ? uploadImageFile.replace('file:', '')
      : uploadImageFile;
  // console.log(uploadUri);
  const data = new FormData();
  data.append('file', {
    uri: uploadUri,
    name: 'file',
    type: 'image/jpg',
  });
  Axios.post(`${API.BASE_URL}/user/image`, data, {
    headers: {
      Authorization: token,
      'Content-Type': 'multipart/form-data',
    },
  })
    .then((res) => {
      setActivity(false);
      setIsImageEdit(false);
      console.log('success');
    })
    .catch((error) => {
      setActivity(false);
      if (error.response) {
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        console.log(error.request);
      } else {
        console.log('Error', error.message);
      }
      setIsImageEdit(false);
    });

The error response I get is我得到的错误响应是

{"DONE": 4, "HEADERS_RECEIVED": 2, "LOADING": 3, "OPENED": 1, "UNSENT": 0, "_aborted": false, "_cachedResponse": undefined, "_hasError": true, "_headers": {"accept": "application/json, text/plain, */*", "authorization": "7aeb23f62bd748c39c9e8652eb1056e2"}, "_incrementalEvents": false, "_lowerCaseResponseHeaders": {}, "_method": "POST", "_perfKey": "network_XMLHttpRequest_https://capi-test.beebl.io/user/image", "_requestId": null, "_response": "Could not retrieve file for uri /storage/emulated/0/Pictures/images/image-448777a5-1734-41f4-857e-a99d5239c279.jpg", "_responseType": "", "_sent": true, "_subscriptions": [], "_timedOut": false, "_trackingName": "unknown", "_url": "https://capi-test.beebl.io/user/image", "readyState": 4, "responseHeaders": undefined, "status": 0, "timeout": 0, "upload": {}, "withCredentials": true}

"_response": "Could not retrieve file for uri /storage/emulated/0/Pictures/images/image-448777a5-1734-41f4-857e-a99d5239c279.jpg" "_response": "无法检索 uri /storage/emulated/0/Pictures/images/image-448777a5-1734-41f4-857e-a99d5239c279.jpg 的文件"

I fixed it by adding我通过添加来修复它

android:requestLegacyExternalStorage="true"

in AndroidManifest.xml在AndroidManifest.xml

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

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