在 React Native [Firebase 9.6.2] 中将图像上传到 firebase 存储时出错

Error uploading image to firebase storage in React Native [ Firebase 9.6.2]

所以我正在尝试将图像上传到 firebase 存储(源文件目录中的本地照片 - 路径:与 .js 相同的路径)。问题是在 firebase 存储中,图像似乎已损坏,每张图片都有 9 个字节 firebase storage image . The authentication and firestore works perfectly, this is my configuration file: firebase config file,这是代码:

const uploadPhoto = async() => {
    // console.log(image);
    // const uploadUri = image;
    // let filename = uploadUri.substring(uploadUri.lastIndexOf('/') + 1);
     
    const metadata = {
      //path: '../../firebase_image.jpg',
      contentType: 'image/jpeg'
    };
    const photog = `./photo.jpg`;
    console.log(photog);
    console.log(uploadUri);
    const storageRef;
    storageRef=ref(storage, 'photogra.jpg');//filename+
    uploadBytes(storageRef, photog, metadata).then((snapshot) => {
      console.log('Uploaded a blob or file!');
    });
   
  }

如果您检查 9 字节文件的内容,它应该是 "photo.jpg"

由于您将 "photo.jpg" 传递给 uploadBytes,它会上传该字符串作为新文件的内容。它不知道如何在该路径加载文件。

您需要传递本地 FileBuffer(您通常会从文件选择器或类似工具中获取),或者自己从文件中加载数据并将内容传递给 uploadBytes.

希望你一切都好!

我从这个很棒的教程中获得了信息并且对我来说效果很好 https://www.youtube.com/watch?v=H-yXO46WDak&lc=z22ph5dhssfqufkcxacdp430segloszlmvuqlp1seplw03c010c

试试这个:

const uploadImageFirebase = async () =>{

  const nameImage =  new Date().toISOString();
  
  
  const img = await fetch(image);
  const bytes = await img.blob();

  try {
    await uploadBytes(ref_con, bytes); 

    } catch (error) {
    console.log(error);
    } finally {
      //
    }        

};