How to upload images to firebase using expo?

788 Views Asked by At

This is my code I took from github expo examples https://github.com/expo/examples/tree/master/with-firebase-storage-upload to upload images to Firebase storage.

 const pickImage = async () => {
        let pickerResult = await ImagePicker.launchImageLibraryAsync({
          allowsEditing: true,
          aspect: [4, 3],
        });
    
            handleImagePicked(pickerResult);
      };
      

      const handleImagePicked = async (pickerResult) => {
        try {
          
    
          if (!pickerResult.cancelled) {
             await uploadImageAsync(pickerResult.uri);
             console.log('done')
          }
        } catch (e) {
          console.log(e);
          alert('Upload failed, sorry :(');
        } finally {
        }
      };
    
      async function uploadImageAsync(uri) {
        // Why are we using XMLHttpRequest? See:
        // https://github.com/expo/expo/issues/2402#issuecomment-443726662
        const blob = await new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.onload = function() {
            resolve(xhr.response);
          };
          xhr.onerror = function(e) {
            console.log(e);
            reject(new TypeError('Network request failed'));
          };
          xhr.responseType = 'blob';
          xhr.open('GET', uri, true);
          xhr.send(null);
        });
      
        const ref = firebase
          .storage()
          .ref()
          .child("images"+Math.random());
        const snapshot = await ref.put(blob);
      
        // We're done with the blob, close and release it
        blob.close();
      
        return await snapshot.ref.getDownloadURL();
      }

It is working fine on Android, but on ios it gives an error saying Event {isTrusted: false}, network request failed. Thank you in advance

1

There are 1 best solutions below

0
On

this was a regression in react-native, it was fixed in a recent patch release: https://github.com/expo/expo/issues/10464#issuecomment-703178030

to get the fix:

  • Download the latest iOS and Android clients from the stores.
  • Install the latest iOS and Android clients with expo client:install:ios and expo client:install:android