如何使用react native和expo将图像上传到firebase存储?

时间:2018-01-05 12:10:26

标签: firebase image-uploading firebase-storage react-native-ios expo

我正在尝试设置个人资料照片并使用react native将其上传到我的ios应用中的firebase存储。我使用以下代码上传图片: -

  _pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      allowsEditing: true,
      aspect: [4, 3]
    });

    this.setState({ image: result.uri });
    console.log(result);

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

    let localUri = result.uri;
    let filename = localUri.split("/").pop();

    // Infer the type of the image
    let match = /\.(\w+)$/.exec(filename);
    let type = match ? `image/${match[1]}` : `image`;

    // Upload the image using the fetch and FormData APIs
    let formData = new FormData();
    // Assume "photo" is the name of the form field the server expects
    formData.append("photo", { uri: localUri, name: filename, type });

    return await fetch(
      "SERVER_URL",
      {
        method: "POST",
        body: formData,
        header: {
          "content-type": "multipart/form-data"
        }
      }
    );
  };

我已经为firebase存储设置了如下规则: -

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

但没有图片上传到该文件夹​​,我没有错误。任何帮助或建议表示赞赏。谢谢。

0 个答案:

没有答案