AngularFire2-Firebase存储getDownloadURL()-如何返回Firestore的URL [英] AngularFire2 - Firebase storage getDownloadURL() - How to return the url for firestore

查看:110
本文介绍了AngularFire2-Firebase存储getDownloadURL()-如何返回Firestore的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览angularfire2文档,以从存储中检索downloadURl.我希望这里缺少一些简单的东西.

I've been going through the angularfire2 documentation to retrieve a downloadURl from storage. I'm hoping I'm missing something simple here.

文档指出:

@Component({
  selector: 'app-root',
  template: `<img [src]="profileUrl | async" />`
})
 export class AppComponent {
   profileUrl: Observable<string | null>;
   constructor(private storage: AngularFireStorage) {
   const ref = this.storage.ref('users/davideast.jpg');
   this.profileUrl = ref.getDownloadURL();
 }
}

但是,一旦我上传了图片,我想以字符串形式返回下载网址以上传到firestore.我需要外部服务的下载URL.

However, once I've uploaded an image I want to return the download url as a string to upload to firestore. I need the download URL for an external service.

我的功能

uploadImage(base64data) {

  const filePath = (`myURL/photo.jpg`);
  const storageRef = firebase.storage().ref();

  var metadata = {
    contentType: 'image',
    cacheControl: "public, max-age=31536000",
  };

  const ref = this.storage.ref(filePath);
  const task = ref.putString(base64data, 'data_url', metadata).then(() => {

    var downloadURL = ref.getDownloadURL();

  })

}

这样可以完美上传图像.但是,我想将下载URL写入firestore.当控制台记录我的"downloadURL"变量时,我得到以下信息:

This uploads the image perfectly fine. However, I would then like to write the download URL to firestore. When console logging my 'downloadURL' variable, I get the following:

PromiseObservable {_isScalar:false,promise:y,调度程序:undefined}

PromiseObservable {_isScalar: false, promise: y, scheduler: undefined}

下载内容位于promise中,可观察到.如何仅将下载URL字符串作为变量?一旦有了,我就可以整理出Firestore更新了.

The download is inside the promise observable. How do I just get the download URL string as my variable? Once I have that I can sort the firestore updates out.

推荐答案

此答案与Firebase 5.0版本无关,他们从上传任务中删除了downloadURL().请参考文档.

一旦上传完成,.downloadURL()观察对象将发出下载URL字符串.然后,您需要订阅以获取该值.

The .downloadURL() observable emits the download URL string once the upload is completed. Then you need to subscribe to get the value.

uploadImage(base64data) {

  const filePath = (`myURL/photo.jpg`);
  //const storageRef = firebase.storage().ref();

  var metadata = {
    contentType: 'image',
    cacheControl: "public, max-age=31536000",
  };

  const ref = this.storage.ref(filePath);
  const task = ref.putString(base64data, 'data_url', metadata);
  const downloadURL = task.downloadURL();

  downloadURL.subscribe(url=>{
     if(url){
         console.log(url);
         //wirte the url to firestore
     }
  })

}

希望这会有所帮助.检查此博客了解更多的详细信息

Hope this helps. check this blog for more detail

这篇关于AngularFire2-Firebase存储getDownloadURL()-如何返回Firestore的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆