繁体   English   中英

FireBase Storage downloadURL在Ionic 3中未设置正确的值

[英]FireBase Storage downloadURL is not setting the right value in Ionic 3

我使用下面的代码块将音频文件发送到FireBase Storage,然后将这些记录添加到表中以保留每个帖子记录的下载链接。

private doPost() {
    const fileName = this.navParams.get('name').replace(/^.*[\\\/]/, '');
    const filePath = 'file://' + this.navParams.get('path');
    const fullPath = this.navParams.get('name');

    this.file.resolveDirectoryUrl(filePath).then((rootDir) => {
      this.file.getFile(rootDir, fileName, { create: false }).then((fileEntry) => {
        fileEntry.file((file) => {
          const reader = new FileReader();
          reader.onloadend = (res: any) => {
            let result = res.target.result
            let blob = new Blob([new Uint8Array(result)], { type: 'audio/mp3' })
            const upload = this.storage.ref('files/' + fileName).put(blob);
            console.log("download url " + upload.downloadURL);
            const postModel = {
              uID: this.uID,
              text: this.postText,
              time: new Date().getTime().toString(),
              downloadURL: upload.downloadURL.toString()
            }
            this.dataProvider.add(postModel, '/Post/');
            // this.dataProvider.addPost(postModel);
          }
          reader.readAsArrayBuffer(file);
        })
      })
      .catch((err) => {
        console.log("Get File'da hata: " + err)
      })
    })
    .catch((err) => {
      console.log("Directory Hatalı!");
    })
  }

但是downloadURL值不是正确的值,这是此代码块设置的记录

downloadURL: "function () { return Object(__WEBPACK_IMPORTED_MODULE_2_rxjs_observable_from__[\"from\"])(task.then(function (s) { return s.downloadURL; })); }"

成功上传后, upload.downloadURL会发出字符串类型的Observable。 首先订阅以获取url,然后进行add

尝试

upload.downloadURL().subscribe(url=>{
    if(url){
        const postModel = {
          uID: this.uID,
          text: this.postText,
          time: new Date().getTime().toString(),
          downloadURL: url
        }
        this.dataProvider.add(postModel, '/Post/');
        // this.dataProvider.addPost(postModel);
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM