简体   繁体   中英

How do I upload an image taken by React-Native-camera to 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})
    }

  }

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM