如何将 React-Native-camera 拍摄的图像上传到 Firebase 存储?

Shu*_*sht 3 firebase react-native react-native-camera firebase-storage

  takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true, mirrorImage: true };
      const data = await this.camera.takePictureAsync(options)

      this.setState({path: data.uri})
    }

  }
Run Code Online (Sandbox Code Playgroud)

takePicture 是捕获图像的函数。现在我想保存捕获的图像并将其上传到 Firebase 存储。

this.setState({path: data.uri}) 更新单击图像位置的路径。

如何使用 React Native fetch Blob 将图像上传到 Firebase 存储中?请帮忙

我目前正在使用react-native-camera - ^1.6.3 和 firebase - ^5.0.3

Moh*_*ouz 5

您可以使用此示例代码将您的图片上传到特定的 api。

var data = new FormData();

data.append('file', { uri: data.uri, name: 'picture.jpg', type: 'image/jpg' });
// Create the config object for the POST
    const config = {
        method: 'POST',
        body: data
    };
    fetch('URL', config).then(responseData => {
        // Log the response form the server // Here we get what we sent to Postman 
     back
        console.log(responseData);
    })
    .catch(err => { console.log(err); });
Run Code Online (Sandbox Code Playgroud)

希望这对您有帮助。