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

问题描述 投票:0回答:1

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

javascript firebase react-native expo javascript-framework
1个回答
0
投票

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

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)
}
}
© www.soinside.com 2019 - 2024. All rights reserved.