简体   繁体   English

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

[英]Uploading an mp3 to Firebase Storage with React Native Expo

I am attempting to upload an mp3 to firebase storage using expo and react native.我正在尝试使用 expo 将 mp3 上传到 firebase 存储并做出原生反应。 So far I've got the file into firebase storage, but it's only 9bytes large, so I'm doing something wrong.到目前为止,我已经将文件放入 firebase 存储空间,但它只有 9 个字节大,所以我做错了什么。 I've attempted this with blob as shown below with no success.我尝试使用 blob 进行此操作,如下所示,但没有成功。

Here is a screenshot of the firebase storage folder showing the file uploaded but not the data of said file:这是 firebase 存储文件夹的屏幕截图,显示了上传的文件,但没有显示该文件的数据:

Firebase 存储图像

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);
    }

  }

The fetch() returns a Promise so you should add an await for that as well. fetch()返回一个 Promise 所以你也应该为它添加一个 await 。

const response = await fetch(result.uri);

Then try using blob() method on the Response :然后尝试在Response上使用blob()方法:

const file = await response.blob()

The third param in uploadBytes should be upload metadata object but you can skip that here: uploadBytes中的第三个参数应该是上传元数据 object 但你可以在这里跳过它:

const snapshot = await uploadBytes(storageRef, file).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native Expo ImagePicker --> Blob --> Firebase 存储不上传 - React Native Expo ImagePicker --> Blob --> Firebase Storage not uploading 使用 expo 和 React Native 将图像上传到 Firebase 存储 (v 9.xx)。 (参考未定义?) - Uploading images to Firebase Storage (v 9.xx) using expo and React Native. (Ref undefined?) 将视频上传到本机反应中的 firebase 存储 - uploading video to firebase storage in react native Expo Camera Photo Upload To Firebase Storage 未定义 - React Native - Expo Camera Photo Upload To Firebase Storage is undefined - React Native 无法从 firebase/storage 导入 { getStorage } 以与 React Native Expo 图像选择器一起使用 - Cannot import { getStorage } from firebase/storage to use with React Native Expo image picker 如何使用 react-native 和 expo 将 m4a 音频文件上传到 Firebase 存储? - How does one upload an m4a audio file to Firebase Storage using react-native and expo? React Native expo 图像选择器上传图像到 firebase 存储 (v9) 崩溃 - React Native expo image picker upload image to firebase storage (v9) crash 在 firebase 上传图片 expo react native - Upload image in firebase expo react native Firebase 使用 React Native 进行电话身份验证 Expo - Firebase Phone Auth with React Native Expo 将 firebase 导入 React Native 时出错 Expo - Error importing firebase to React Native Expo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM