Firebase存储 - 将图像上传到firebase存储,并在实时数据库

时间:2018-03-14 20:34:15

标签: firebase firebase-realtime-database firebase-storage

我无法将图片上传到firebase存储空间。我需要在HTML中使用一种文件类型的输入对象。然后我需要为该输入选择的图像上传到我的项目的firebase存储。是否还有一种方法可以将该图像的位置存储在实时数据库中,以便以后访问它?我该怎么做?

提前谢谢

2 个答案:

答案 0 :(得分:3)

链接到数据库试试这个:

var ref= firebase.database().ref("Uploads");
var storage = firebase.storage();
var pathReference = storage.ref('images/stars.jpg');
pathReference.getDownloadURL().then(function(url) {
ref.push().set({
imgurl: url
});

更多信息:

https://firebase.google.com/docs/storage/web/download-files

https://firebase.google.com/docs/storage/web/upload-files

将其添加到存储后,您将能够使用url获取getDownloadUrl(),然后将其添加到数据库中:

Uploads
  pushid
     imgurl: url

答案 1 :(得分:0)

使用 Firestore(存储)检查图像文件,并使用 Cloud Storage(数据库)保存图像路径,名称和文件大小。

enter image description here

上传图片

// The main task
this.task = this.storage.upload(path, file, { customMetadata });

保存图像信息

  addImagetoDB(image: MyData) {
    //Create an ID for document
    const id = this.database.createId();

    //Set document id with value in database
    this.imageCollection.doc(id).set(image).then(resp => {
      console.log(resp);
    }).catch(error => {
     console.log("error " + error);
    });
  }

源教程Link