简体   繁体   English

Angular Firebase 存储:等待照片上传返回downloadURL

[英]Angular Firebase Storage: wait for photo upload to return downloadURL

I've tried everything and I feel like I'm about to lose my mind.我已经尝试了一切,我觉得我快要失去理智了。 I'm uploading signature photo as base64 to Firebase Storage using putString and I'm getting downloadURL as a return.我正在使用 putString 将签名照片作为 base64 上传到 Firebase 存储,并且我得到了 downloadURL 作为回报。 The problem is that sometimes it returns blank string, because uploading isn't finished, and the method goes on.问题是有时它会返回空白字符串,因为上传尚未完成,并且该方法继续进行。 So when I try to invoke this method via button I have to click it 2 times or just wait couple seconds to do so.因此,当我尝试通过按钮调用此方法时,我必须单击它 2 次或等待几秒钟。

async saveCanvas() {
const loading = await this.loadingController.create();
await loading.present();
var signature = this.signaturePad.toDataURL().slice(22);
this.signaturePhotoName = new Date().toJSON();
const fileStoragePath = `signatures/${this.signaturePhotoName}`;
const imageFireStorageReference = this.angularFireStorage.ref(fileStoragePath);
this.fireUploadTask = this.angularFireStorage.ref(fileStoragePath).putString(signature, 'base64', { contentType: 'image/png'});
this.fireUploadTask.snapshotChanges().pipe(
  finalize(() => {
    const downloadURL = imageFireStorageReference.getDownloadURL();
    downloadURL.subscribe(async url => {
      if(url) {
        this.uploadedSignaturePhotoPath = url;
        this.report.signature = this.uploadedSignaturePhotoPath;
        this.reportForm.get('_signature').setValue(this.uploadedSignaturePhotoPath);
        await loading.dismiss();
      }
    })
  })
).subscribe();}

async onSubmit() {
    if(!this.isCanvasBlank()) {
      this.saveCanvas().then(() => {
        if(this.report.signature != '') {
          if(this.reportForm.valid) {
            this.reportsService.postReport(this.report).subscribe(response => {
              console.log(response);
            })}
          else {
            console.log("need more info");
          }
        }
      });
    } else {
      alert("no signature!");
    }
  }

Await for upload to finish with putString , then go to query the download url getDownloadURL() .等待上传完成putString ,然后 go 查询下载 url getDownloadURL()

this.fireUploadTask = await this.angularFireStorage.ref(fileStoragePath)
     .putString(signature, 'base64', { contentType: 'image/png'});

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

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