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

查看:31
本文介绍了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();
 }
}

但是,一旦我上传了图片,我想将下载 url 作为字符串返回以上传到 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, scheduler: undefined}

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

下载位于可观察的承诺内.我如何只获取下载 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(). 请参考 doc.

一旦上传完成,.downloadURL() observable 就会发出下载 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天全站免登陆