reactjs 如何从firebase storage/react native获取URL

bihw5rsg  于 2023-05-06  发布在  React
关注(0)|答案(1)|浏览(105)

当用户从相册中选择一张照片时,我想得到的主要内容是从该照片中获得一个URL,以便我可以在用户数据库中使用。把URL放到firebase中的用户文档是有效的,ImagePicker也是有效的。我的主要问题是我如何(通过Firebase存储)获得实际的URL,而不是像这样的东西:
250F8B0C-72D3-43D4-9CDA-B7F6D8A11F81.jpg

验证码:

const [image, setImage] = useState("");
  //functions
  const pickImage = async () => {
    try {
 // No permissions request is necessary for launching the image library
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });

   console.log(result);

    if (!result.canceled) {
      auth.currentUser.photoURL = result.uri
      setImage(result.uri);
      console.log(result.uri)
      imgFirebase()
    }
  {/**
  await updateDoc(doc(db, "users", uid, {
      photoURL: result.uri.toString()
    }))
*/}
    }
    catch(E) {
      alert(E)
    }

  };
  async function imgFirebase () {
   const d = await fetch(image)
   const dd = await d.blob()
   const fileName = image.substring(image.lastIndexOf("/")+1)
   console.log(fileName)
   const storage = getStorage();
  const storageRef = ref(storage, fileName);
  uploadBytes(storageRef,dd).then((snapshot) => {
    console.log('Uploaded a blob or file!');
  });
  const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');  

  }

存储结构what the firebase storage looks like:**注意,如果我按下图像,它实际上会在新选项卡中给我URL......我已经尝试了文档中的getDownloadUrl函数,但它也给了我存储引用的错误
在我的imgFirebase函数中,我需要得到什么来获得从图像选择器/firebase存储中获得的img/blob的URL

cs7cruho

cs7cruho1#

如何在图片上传后获取url

reader.onload = (e) => {
                if (file == null) return;
                Swal.fire({
                    title: 'Your uploaded picture',
                    imageUrl: e.target.result,
                    imageAlt: 'The uploaded picture'
                })
                const imageRef = ref(storage, `user/profile/${file.nama + user.uid}`);
                uploadBytes(imageRef, file).then((snapshot) => {
                    getDownloadURL(snapshot.ref).then((url) => {
                        setImageUrls(url);
                    });
                });
            }

相关问题