重新加载后,本地URI(file:/// […] / cache / […] .jpg)中的图像未显示在网络应用中

时间:2019-03-21 05:26:53

标签: react-native expo

我正在尝试使用expo-cli将照片发送到我的网络应用程序。我的网络应用上的头像图片为http://i.imgur.com/FTa2JWD.png。该代码运行没有错误。发布请求后的头像图像现在是我的本地URI图像文件(file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252FAtten-a0825f52-c5c2-48f4-86e0- b03730098c8b / ImagePicker / e600efc4-7623-4dea-bac8-1fc4033e86bb.jpg),这是我在uploadImageAsynce函数中执行console.log(formBody12)之后的结果。但是重新加载后,Web应用程序的头像图像未显示。我该如何解决这个问题? 这是我的代码:

  _pickImage = async () => {
    const { status: cameraRollPerm } = await Permissions.askAsync(
      Permissions.CAMERA_ROLL
    );

    if (cameraRollPerm === "granted") {
      let pickerResult = await ImagePicker.launchImageLibraryAsync({
        allowsEditing: true,
        aspect: [4, 3]
      });

      this._handleImagePicked(pickerResult);
    }
  };
  _handleImagePicked = async pickerResult => {
    let uploadResponse;

    try {
      this.setState({
        uploading: true
      });

      if (!pickerResult.cancelled) {
        uploadResponse = await uploadImageAsync(
          this.state.userToken,
          this.state.userID,
          this.state.userFirstName,
          this.state.userLastName,
          this.state.userEmail,
          this.state.userPhone,
          pickerResult.uri
        );

        this.setState({
          image: pickerResult.uri
        });
      }
    } catch (e) {
      console.log({ uploadResponse });
      console.log({ e });
      alert("Upload failed, sorry :(");
    } finally {
      this.setState({
        uploading: false
      });
    }
  };

这是我的帖子请求:

async function uploadImageAsync(
  userToken,
  userID,
  userFirstName,
  userLastName,
  userEmail,
  userPhone,
  image
) {
  let apiUrl =
    "https://deployattendancemanagement.herokuapp.com/api/student/update";
  const formBody1 = "token=";
  const formBody2 = userToken;
  const formBody3 = "&id=";
  const formBody4 = userID;
  const formBody5 = "&name=";
  const firstName = userFirstName;
  const lastName = userLastName;
  const formBody6 = firstName.concat(" ", lastName);
  const formBody7 = "&email=";
  const formBody8 = userEmail;
  const formBody9 = "&phone=";
  const formBody10 = userPhone;
  const formBody11 = "&avatar=";
  var formBody12 = image;
  console.log(formBody12)
  const formBody = formBody1.concat(
    "",
    formBody2,
    formBody3,
    formBody4,
    formBody5,
    formBody6,
    formBody7,
    formBody8,
    formBody9,
    formBody10,
    formBody11,
    formBody12
  );
  let options = {
    method: "PUT",
    body: formBody,
    headers: {
      Accept: "application/json",
      "Content-Type": "application/x-www-form-urlencoded"
    }
  };

  return fetch(apiUrl, options)
    .then(response => response.json())
    .then(res => {
      if (typeof res.success != "undefined") {
        console.log("Error!", "Error: " + res.message);
      } else {
        console.log("Success");
      }
    })
    .catch(error => {
      console.error(error);
    });
}

Edit1:我在此链接Github

中遇到了与此人相同的问题

0 个答案:

没有答案