将MP3上传到使用React Native Expo的Firebase存储

发布于 2025-01-24 14:14:48 字数 1278 浏览 1 评论 0 原文

我正在尝试使用Expo上传MP3到Firebase存储,并进行反应本机。到目前为止,我将文件放入了Firebase存储中,但是它只有9个比例,所以我做错了什么。如下所示,我尝试使用斑点尝试了一下。

这是firebase存储文件夹的屏幕截图,显示上传的文件,但不是所述文件的数据:

“

任何帮助非常感谢,我觉得我错过了与文件一起实际上上传数据的步骤。

export default function SongPicker() {
  const [song, setSong] = useState(null);

  //Get current user through authentication
  const user = auth.currentUser;

  const pickDocument = async () => {
    let result = await DocumentPicker.getDocumentAsync({});
    // Fetch the photo with it's local URI
    const response = fetch(result.uri);
    alert(result.uri);
    console.log(result);

    const file = new Blob(
      [response.value], {
        type: 'audio/mpeg'
      });
    console.log('do we see this?');

    try {
      //Create the file reference
      const storage = getStorage();
      const storageRef = ref(storage, `songs/${user.uid}/${result.name}`);

      // Upload Blob file to Firebase
      const snapshot = uploadBytes(storageRef, file, 'blob').then((snapshot) => {
        console.log('Uploaded a song to firebase storage!');
      });

      setSong(result.uri);
    } catch (error) {
      console.log(error);
    }

  }

I am attempting to upload an mp3 to firebase storage using expo and react native. So far I've got the file into firebase storage, but it's only 9bytes large, so I'm doing something wrong. I've attempted this with blob as shown below with no success.

Here is a screenshot of the firebase storage folder showing the file uploaded but not the data of said file:

Firebase Storage Image

Any help is greatly appreciated, I feel like I'm missing a step to actually upload the data along with the file.

export default function SongPicker() {
  const [song, setSong] = useState(null);

  //Get current user through authentication
  const user = auth.currentUser;

  const pickDocument = async () => {
    let result = await DocumentPicker.getDocumentAsync({});
    // Fetch the photo with it's local URI
    const response = fetch(result.uri);
    alert(result.uri);
    console.log(result);

    const file = new Blob(
      [response.value], {
        type: 'audio/mpeg'
      });
    console.log('do we see this?');

    try {
      //Create the file reference
      const storage = getStorage();
      const storageRef = ref(storage, `songs/${user.uid}/${result.name}`);

      // Upload Blob file to Firebase
      const snapshot = uploadBytes(storageRef, file, 'blob').then((snapshot) => {
        console.log('Uploaded a song to firebase storage!');
      });

      setSong(result.uri);
    } catch (error) {
      console.log(error);
    }

  }

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

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

发布评论

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

评论(1

蝶舞 2025-01-31 14:14:48

fetch()返回诺言,因此您也应该添加等待此期待。

const response = await fetch(result.uri);

然后尝试在 blob()方法“>响应

const file = await response.blob()

uploadbytes 中的第三个参数应该是上传元数据对象,但您可以在此处跳过:

const snapshot = await uploadBytes(storageRef, file).

The fetch() returns a Promise so you should add an await for that as well.

const response = await fetch(result.uri);

Then try using blob() method on the Response:

const file = await response.blob()

The third param in uploadBytes should be upload metadata object but you can skip that here:

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