如何使用Angularfire2将mp3文件上传到Firebase存储?

时间:2017-11-28 08:15:42

标签: javascript angular firebase ionic2 angularfire2

我试图在我的ionic2应用程序中使用angularfire2将mp3文件上传到firebase存储,

  

此文件是使用ionicFile(path,fileName,replace)使用ionic-native / file插件创建的,其中doc为here

这是代码:

this.file.createFile(cordova.file.externalDataDirectory, this.fileName, true).then((result) => {
                console.log(JSON.stringify(result,null,2),"result")
                this.files = result;
                this.mediaPlugin = new MediaPlugin(cordova.file.documentsDirectory + this.fileName);
            }, (e) => {
              console.log(JSON.stringify(e, null, 2))
            })

此代码运行良好,文件已创建!

之后我想把这个文件放到Firebase存储中,我尝试了这段代码但是没有用:

let storageRef = firebase.storage().ref();
    const voiceRef = storageRef.child(`voices/${this.fileName}.wav`);
    voiceRef.put(this.files.filesystem).then(snap=>{
      console.log(JSON.stringify(snap));
    },(e)=>{
      console.log(JSON.stringify(e,null,2));
    })

我收到一条错误消息:

firebase error

我认为错误来自put方法参数,我不知道该放在这个参数中。

感谢。

1 个答案:

答案 0 :(得分:0)

在上传文件之前尝试specifying the content type

let metadata = {
  contentType: 'audio/mpeg',
};
voiceRef.put(this.files.filesystem, metadata).then(...)