首页 > 解决方案 > 如何使用图像选择器(EXPO)生成的本地 uri 将图像上传到服务器?

问题描述

我正在尝试将图像或文件从移动设备发送到后端,但图像选择器无法为我提供图像提供的 URI 无法识别。

_pickImage = async () => {
let result = await DocumentPicker.getDocumentAsync({});

console.log(result);

if (!result.cancelled) {
  this.setState({ files: result.uri });

  // upload file && get File Id
  axios({
    method: "post",
    url: "https://someurl",
    data: {
      image: this.state.file
    },
    headers: {
      "Content-Type": "multipart/form-data",
      // TODO: upadte with dynamic token
      Authorization:
        "Bearer {token}"
    }
  }).then(
    function(response) {
      console.log(response.data.message);
      // this.setState({
      //   hospItems: response.data.data
      // });
    }.bind(this)
  );
}

};

控制台.log(结果);返回:: Object { "name": "1554736582840.jpg", "size": 45240, "type": "success", "uri": "file:///data/user/0/host.exp.exponent /cache/ExperienceData/%2540abhinavofficial%252Fmobi-medic/DocumentPicker/cd34bbd8-7c0c-414a-a76d-8d06e2abf670jpg", }

来自服务器的响应:图像字段是必需的。

标签: javascriptreact-nativeexpo

解决方案


推荐阅读