【问题标题】:FireBase Storage downloadURL is not setting the right value in Ionic 3FireBase 存储下载 URL 未在 Ionic 3 中设置正确的值
【发布时间】:2018-09-16 06:42:39
【问题描述】:

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

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

【问题讨论】:

    标签: angular firebase ionic3 firebase-storage


    【解决方案1】:

    upload.downloadURL 上传成功后发出字符串类型的 Observable。首先订阅获取网址,然后执行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);
        }
    }
    

    【讨论】:

    • 谢谢。如果我再次成功地在设备上运行我的应用程序,我会尝试一下 :)
    猜你喜欢
    • 2018-08-09
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2020-04-08
    • 2017-06-07
    • 2020-11-22
    相关资源
    最近更新 更多