不存在React Native Blob数据发布请求

时间:2020-05-23 21:33:15

标签: spring-boot react-native axios fetch

我正在尝试将照片上传到Spring Boot应用程序:

控制器:

  @PostMapping
  public ResponseEntity<?> createPhoto(@RequestPart(value = "file") MultipartFile file) {
    photoService.createPhoto(file);
    return new ResponseEntity<>(HttpStatus.CREATED);

反应面:

image对象来自react-native-image-picker,形式如下:

image= {uri: 'content://com.google.android.apps.photos...', data: '/9j/4Q...'}

这是请求方法:

export const uploadImage = image => dispatch => {

  const body = new FormData();
  const blob = new Blob([image.data], {
    type: 'data:image/jpeg;base64',
  });

  body.append('file', blob);
  fetch("'http://10.0.2.2:8110/photos", {
    method: 'POST',
    body,
  })
    .then(res => res.json())
    .then(res => console.log(res));
};

错误是:

TypeError: Network request failed
    at EventTarget.xhr.onerror (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28007:18)
    at EventTarget.dispatchEvent (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:34135:27)
    at EventTarget.setReadyState (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33219:20)
    at EventTarget.__didCompleteResponse (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33046:16)
    at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33156:47
    at RCTDeviceEventEmitter.emit (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3422:37)
    at MessageQueue.__callFunction (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2750:44)
    at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2472:17
    at MessageQueue.__guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2704:13)
    at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2471:14)

请求甚至没有到达后端api。它在设备中失败。错误对象中没有响应。我还检查了网络中不存在该请求。

AndroidManifest.xml

<application
      android:name=".MainApplication"
      //...
      android:usesCleartextTraffic="true"
      >

0 个答案:

没有答案