反应本地上传图像到api网络出现错误

时间:2020-04-10 19:17:12

标签: android reactjs react-native file-upload multipartform-data

您好,我正在尝试使用表单数据将图像上传到api,但出现网络错误

为什么我遇到此错误,我尝试手动放置图像类型,但仍然无法从邮递员那里正常工作


 async ImageFormData() {
    const {photo} = this.state;
    console.log(photo);
    const formData = new FormData();
    formData.append('file', {
      uri: photo.uri,
      type: 'image/jpeg',
      name: photo.name,
    });

    setTimeout(() => {
      this.setState({isLoading: true}, () => {
        this.uploadImage(formData);
      });
    }, 100);
  }

  async uploadImage(formData) {
    console.log(formData);
    const {cid, email, phone, name} = this.state;
    console.log(cid, email, phone, name);
    fetch(
      'api,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        body: formData,
      },
    )
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(responseJson);
        this.info();
      })
      .catch((error) => {
        console.log(error);
      });
  }
}

TypeError:网络请求失败 在EventTarget.xhr.onerror(C:\ feras \ worker \ node_modules \ whatwg-fetch \ dist \ fetch.umd.js:473) 在EventTarget.dispatchEvent(C:\ feras \ worker \ node_modules \ event-target-shim \ dist \ event-target-shim.js:818) 在EventTarget.setReadyState(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:575) 在EventTarget .__ didCompleteResponse(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:389) 在C:\ feras \ worker \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:502 在RCTDeviceEventEmitter.emit(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ vendor \ emitter \ EventEmitter.js:189) 在MessageQueue .__ callFunction(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:425)中 在C:\ feras \ worker \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:112 在MessageQueue .__ guard(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:373) 在MessageQueue.callFunctionReturnFlushedQueue(C:\ feras \ worker \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:111)

0 个答案:

没有答案