我尝试使用React Native“expo”将多个图像上传到Firebase存储和Firebase数据库集合,但这很困难,我什至尝试使用chatGpt,但生成的代码令人困惑并且不知何故过时,因此它不起作用
我为此编写了一个函数。这里是:
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)
}
}