反应 - 将化身上传到firebase

发布于 2025-01-25 11:40:51 字数 653 浏览 3 评论 0原文

下午好,我不知道如何将照片(Avatar)上传到Firebase,但是每个用户的头像都不同并在每个用户登录处保存。通过Firebase身份验证的用户注册

const [user, loading] = useAuthState(auth);
  const [image , setImage] = useState("");
  const storage = getStorage();

//upload function
  const upload = ()=>{
    const storageRef = ref(storage, `usersAvatar/${image.name}`);

    if(image == null) return;
    uploadBytes(storageRef, image).then(() =>{
      getDownloadURL(storageRef).then((url) => {
        setUrl(url);
      })
    })
  }


// return
<input type="file" onChange={(e)=>{setImage(e.target.files[0])}}/>
<button onClick={upload}>Upload</button>

Good afternoon, I can’t figure out how to upload a photo (avatar) to firebase, but so that the avatar for each user is different and saved at each user login. User registration via firebase Authentication

const [user, loading] = useAuthState(auth);
  const [image , setImage] = useState("");
  const storage = getStorage();

//upload function
  const upload = ()=>{
    const storageRef = ref(storage, `usersAvatar/${image.name}`);

    if(image == null) return;
    uploadBytes(storageRef, image).then(() =>{
      getDownloadURL(storageRef).then((url) => {
        setUrl(url);
      })
    })
  }


// return
<input type="file" onChange={(e)=>{setImage(e.target.files[0])}}/>
<button onClick={upload}>Upload</button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2025-02-01 11:40:51

由于您在云存储中使用此位置来存储用户的图像:

const storageRef = ref(storage, `usersAvatar/${image.name}`);

任何上传相同image.name的用户最终都会覆盖其他任何人的图像。

相反,您要做的是基于用户UID上的上传位置。因此,例如:

const storageRef = ref(storage, `usersAvatar/${user.uid}/${image.name}`);

Since you use this location in Cloud Storage to store the user's image:

const storageRef = ref(storage, `usersAvatar/${image.name}`);

Any user who uploads the same image.name will end up overwriting anyone else's image.

What you'll want to do instead is based the upload location on the UID of the user. So for example:

const storageRef = ref(storage, `usersAvatar/${user.uid}/${image.name}`);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文