React-native-fetch-blob: firebase storage file upload..

Created on 13 Aug 2016  路  6Comments  路  Source: wkh237/react-native-fetch-blob

In React-Native app how can i upload an image from user' device to Firebase storage?

image path -> /storage/emulated/0/Pictures/image-760dac79.jpg

needs feedback

Most helpful comment

Hi @gilra2000 , I think you can check the sample app repository.

Basically you have to replace window.XMLHttpRequest and window.Blob this way

import RNFetchBlob from 'react-native-fetch-blob'

const polyfill = RNFetchBlob.polyfill

window.XMLHttpRequest = polyfill.XMLHttpRequest
window.Blob = polyfill.Blob

Then create a Blob from the path

let path = '/storage/emulated/0/Pictures/image-760dac79.jpg'
Blob.build(RNFetchBlob.wrap(path), { type : 'image/jpeg' })
        .then((blob) => firebase.storage()
                .ref('rn-firebase-upload')
                .child(testImageName)
                .put(blob, { contentType : 'image/png' })
        )
        .then((snapshot) => { /* there we go ! */ })

However, if you replace window.XMLHttpRequest the fetch may not work properly, but you can use our fetch API as an alternative.

All 6 comments

Hi @gilra2000 , I think you can check the sample app repository.

Basically you have to replace window.XMLHttpRequest and window.Blob this way

import RNFetchBlob from 'react-native-fetch-blob'

const polyfill = RNFetchBlob.polyfill

window.XMLHttpRequest = polyfill.XMLHttpRequest
window.Blob = polyfill.Blob

Then create a Blob from the path

let path = '/storage/emulated/0/Pictures/image-760dac79.jpg'
Blob.build(RNFetchBlob.wrap(path), { type : 'image/jpeg' })
        .then((blob) => firebase.storage()
                .ref('rn-firebase-upload')
                .child(testImageName)
                .put(blob, { contentType : 'image/png' })
        )
        .then((snapshot) => { /* there we go ! */ })

However, if you replace window.XMLHttpRequest the fetch may not work properly, but you can use our fetch API as an alternative.

T.T

failed...

Can you help?

ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);

    if (response.didCancel) {
      console.log('User cancelled image picker');
    }
    else if (response.error) {
      console.log('ImagePicker Error: ', response.error);
    }
    else if (response.customButton) {
      console.log('User tapped custom button: ', response.customButton);
    }
    else {

      const testImageName = `image-from-react-native-${Platform.OS}-${new Date()}.jpg`;

      let path = response.path;  
      // path ->  /storage/emulated/0/Pictures/image-8de3ead3-4411cc.jpg

      Blob.build(RNFetchBlob.wrap(path), { type : 'image/jpeg' })
        .then((blob) => firebase.storage()
                .ref('rn-firebase-upload')
                .child(testImageName)
                .put(blob, { contentType : 'image/png' })
        )
        .then((snapshot) => { /* there we go ! */ })

    }
  });

@gilra2000 , is there any error message ? I think I need more information to figure out where's the problem.

2016-08-13 10 36 37

file size.. 4b ;;

@gilra2000 , does this happens on Android ? I've found an Android XMLHttpRequest bug in 0.9.0, that might cause some problem.

If you're still getting problem, please try install the package to latest package 0.9.1 via the following commands

$ rnpm uninstall react-native-fetch-blob
$ npm install --save react-native-fetch-blob
$ rnpm link

Not working just showing page no any action. pls guide me how to save image and videos clicked by camera in database

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yaronlevi picture yaronlevi  路  4Comments

gonglong picture gonglong  路  3Comments

nielsswinkels picture nielsswinkels  路  3Comments

amirfefer picture amirfefer  路  4Comments

mykelaballe picture mykelaballe  路  4Comments