等待firebase存储上传完成后再运行代码以获取下载链接 - angular2 / ionic / angularfire2

时间:2017-07-19 21:06:55

标签: ios angular firebase ionic-framework upload

我使用firebase上传使用相机或照片库拍摄的图像。承诺包含相关代码:

fileEntry.file(function (file) {
  //var reader = new FileReader();
  var reader = new FileReader();

  reader.onload = (e) => {
    var dataURL = reader.result;

    let image       : string  = 'profile_' + self.username + '_' + square + '.jpg',
      storageRef  : any,
      parseUpload : any;

    return new Promise((resolve, reject) => {
      storageRef       = firebase.storage().ref('/profile/' + self.username + '/' + image);
      parseUpload      = storageRef.putString(dataURL, 'data_url');

      parseUpload.on('state_changed', (_snapshot) => {
        // We could log the progress here IF necessary
        console.log('snapshot progess ' + _snapshot);
        },
        (_err) => {
           reject(_err);
           console.log(_err);
        },
        (success) => {
           resolve(parseUpload.snapshot);
        })
      }).then(value => {
        //this.af.list('/profile/' + self.username).push({ pic: image });
      }).catch(function(error) {
        console.log(error.message);
      });

此代码从firebase返回到调用cameraservice的页面控制器中的下载URL:

presentActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      title: 'Choose source',
      buttons: [
        {
          text: 'Camera',
          handler: () => {
            let itemArrayTwo = this.profComponents.toArray();
            this.cameraService.getMedia(this.optionsGetCamera, this.square).then(() => {

                let storageRef = firebase.storage().ref().child('/profile/' + this.username + '/profile_' + this.username + '_' + this.square + '.jpg');
                  storageRef.getDownloadURL().then(url => {
                    console.log(url);
                    this.myrenderer.setElementAttribute(itemArrayTwo[this.square - 1].nativeElement, 'src', url);
                    this.showSquare();
                  });

            }); //pass in square choice
            //this.myrenderer.setElementAttribute(this.itemArrayTwo[this.square - 1].nativeElement, 'src', 'block');
            console.log('camera clicked');
          }

          ...

问题是,返回下载URL的函数在parseUpload.on('state_ ...完成上传之前运行。控制台输出示例:

...

[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: 
            https://firebasestorage.googleapis.com/v0/b/mane-4152c.appspot.com/o/profile%2Fjackson%2Fprofile_jackson_1.jpg?alt=media&token=85884da1-d9f1-4ffb-a4dd-a24ce9bdfbef
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object] 
[16:24:16]  console.log: snapshot progess [object Object]

...

您可以看到在快照上载进度中记录了downloadURL。在做其他任何事情之前,必须等待firebase上传完成。

0 个答案:

没有答案