我试图上传一个图像到Firebase存储和下载的URL,并更新到Firestore,但我不能上传。
const uploadData = async () => {
if (loading) return;
setLoading(true);
// Upload the image blob to Firebase Storage
const response = await fetch(image.uri);
const blob = await response.blob();
console.log(blob);
const storageRef = ref(storage, `users/${uid}/profile-image`);
const uploadTask = uploadBytes(storageRef, blob);
// Get the download URL of the uploaded image
let downloadURL;
try {
const snapshot = await uploadTask;
downloadURL = await getDownloadURL(snapshot.ref);
} catch (error) {
console.error(error);
setLoading(false);
return;
}
// Update the user document in Firestore with the download URL
const userDocRef = doc(firestore, "users", uid, "userdetails");
try {
await updateDoc(userDocRef, {
name: name,
username: username,
email: email,
profileImageURL: downloadURL,
timestamp: serverTimestamp(),
});
} catch (error) {
console.error(error);
setLoading(false);
return;
}
setLoading(false);
};
1条答案
按热度按时间k7fdbhmy1#
可以使用this blog作为参考。
这个repo使用类似的逻辑将图像上传到firebase存储。