【问题标题】:How do I upload an image taken by React-Native-camera to Firebase storage?如何将 React-Native-camera 拍摄的图像上传到 Firebase 存储?
【发布时间】:2019-05-20 17:44:21
【问题描述】:
  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})
    }

  }

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

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

如何使用 react native fetch blob 将我的图像上传到 firebase 存储?请帮忙

我目前正在使用 react-native-camera - ^1.6.3 和火力基地 - ^5.0.3

【问题讨论】:

    标签: firebase react-native firebase-storage react-native-camera


    【解决方案1】:

    您可以使用此示例代码将您的图片上传到特定的 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); });
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:
      async function uploadFile() {
          const data = new FormData();
      
          data.append('file', { uri: data.uri, name: 'anyname.jpg', type: 'image/jpg' });
      
          const response = await axios.post('URL', data);
      
          const { id, url } = response.data;
      
      }
      

      它成功了,谢谢。

      【讨论】:

        猜你喜欢
        • 2017-08-18
        • 2020-08-31
        • 2020-03-06
        • 1970-01-01
        • 2020-03-23
        • 2018-03-14
        • 2021-09-06
        • 2021-11-06
        相关资源
        最近更新 更多