首页  >  问答  >  正文

使用React Native Expo将多个图像上传到Firebase集合和Firebase

我正在尝试使用React Native“expo”将多个图像上传到Firebase存储和Firebase数据库集合,但这很困难,我什至尝试使用chatGpt,但生成的代码令人困惑并且不知何故过时,因此它不起作用在此处输入图像描述

P粉226642568P粉226642568178 天前1477

全部回复(1)我来回复

  • P粉322106755

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

    我为此编写了一个函数。这里是:

    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)
    }
    }

    回复
    0
  • 取消回复