How do I upload an image taken by React-Native-camera to Firebase storage?

◇◆丶佛笑我妖孽 提交于 2021-02-08 06:02:35

问题


  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 is the function to capture the image. Now I want to save the captured image and upload it to Firebase storage.

this.setState({path: data.uri}) updates the path to clicked Image's location.

How do I use react native fetch blob to upload my image in firebase storage? Please help

I'm currently using react-native-camera - ^1.6.3 and firebase - ^5.0.3


回答1:


You can to using this sample code to upload your picture to the specific 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); });

Hope this help you.




回答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;

}

It Dit worked, Thanks.



来源:https://stackoverflow.com/questions/53864677/how-do-i-upload-an-image-taken-by-react-native-camera-to-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!