Home  >  Q&A  >  body text

Upload multiple images to Firebase collection and Firebase using React Native Expo

I'm trying to use React Native "expo" to upload multiple images to Firebase Storage and Firebase Database Collections but it's difficult, I even tried using chatGpt but the generated code is confusing and somehow outdated so It doesn't work Enter image description here

P粉226642568P粉226642568179 days ago1482

reply all(1)I'll reply

  • P粉322106755

    P粉3221067552024-04-05 22:40:27

    I wrote a function for this. here it is:

    import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'
    import { auth, storage } from '../../config/firebase'
    export async function uploadImage(uri) {
    try {
        const response = await fetch(uri)
        const blobFile = await response.blob()
    
        const image_name = 'image_name'
        const metadata = {
            contentType: 'image/jpeg',
            customMetadata: {
                from: auth?.currentUser?.uid
            }
        }
    
        const reference = ref(storage, image_name)
        const result = await uploadBytes(reference, blobFile, metadata)
        const url = await getDownloadURL(result.ref)
    
        return url
    } catch (err) {
        return Promise.reject(err)
    }
    }

    reply
    0
  • Cancelreply