Firebase存储 - 将图像上传到Firebase存储并在实时数据库中创建链接

6
我遇到了上传图片到Firebase存储的问题。我需要在HTML中添加一个类型为文件的输入对象。然后,我需要将选择的图像上传到Firebase存储库中以供我的项目使用。是否还有一种方法可以将该图像的位置存储在实时数据库中,以便以后访问?如何实现呢?
提前致谢。

是的,但我主要是在寻找如何轻松上传图片的信息。 - Benjamin Sommer
1
好的,这个:https://firebase.google.com/docs/storage/web/upload-files 是唯一的方法。在那之后,您可以实现我下面的答案。 - Peter Haddad
请查看此实用程序类,可将图像、视频和文件上传到Firebase存储。 https://handyopinion.com/utility-class-to-upload-images-videos-files-to-firebase-storage-in-swift-ios/ - Asad Ali Choudhry
2个回答

9

要链接到数据库,请尝试以下操作:

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

将文件添加到存储空间后,您可以使用 getDownloadUrl() 获取文件的 url,然后将其添加到数据库中:

Uploads
  pushid
     imgurl: url

我认为你的意思是put(),而不是push()。 - Thomas David Kehoe
不要使用push()来创建随机id,然后将图像置于其下。 - Peter Haddad

5

查看使用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);
    });
  }

源教程链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接