如何将多个图像发布到 firebase 并获取它们的下载 url?

迈克科技

我正在使用 ionic 3 和 firebase。我正在尝试为每个循环一次上传多个图像。上传它们后,我需要获取下载 url 以将它们存储在数据库中。由于获取“下载 url”部分是异步的,我必须在图像上传的 .then() 部分之后将数据插入到数据库中....我该怎么做?这是我提交表单后到目前为止得到的:

post_news(form: NgForm){

    this.title = form.value.title;
    this.content = form.value.content;
    this.category = form.value.category;


    console.log(this.capturedimage1);


    if(this.capturedimage1 !== ''){


        this.images_to_upload.push(this.capturedimage1);


    }



    if(this.capturedimage2 !== ''){

        this.images_to_upload.push(this.capturedimage2);
    }

    if(this.capturedimage3 !== ''){

        this.images_to_upload.push(this.capturedimage3);
    }


    if(this.capturedimage4 !== ''){

        this.images_to_upload.push(this.capturedimage4);
    }



    images_url_from_db = [];

    this.images_to_upload.forEach(function(item){


        let storageRef = firebase.storage().ref();

        const filename = Math.floor(Date.now() / 1000);

        const imageRef = storageRef.child(`images/${filename}.jpg`);





     imageRef.putString(item, firebase.storage.StringFormat.DATA_URL).then(data=>{

     images_url_from_db.push(downloadURL);



     });


    })

}
迈克科技

可能对那里的人有用...玩了很长时间后,我想出了一种方法来做到这一点.....首先,我将非图像数据插入到数据库中,例如(标题,内容,类别)。然后我得到插入数据的 key 。然后我用key一张一张的上传图片,会插入图片url为image1、image2、image3,依此类推...

post_news(form: NgForm){

    this.title = form.value.title;
    this.content = form.value.content;
    this.category = form.value.category;


    this.news = this.afDb.list('/news');
    this.news.push({title: this.title,content: this.content, category: this.category}).then(data=>{

        var item_key = data.key;

        console.log(data.key);

        if(this.capturedimage1 !== ''){

            let storageRef = firebase.storage().ref();

            const filename = Math.floor(Date.now() / 1000);

            const imageRef = storageRef.child(`images/${filename}.jpg`);


            imageRef.putString(this.capturedimage1, firebase.storage.StringFormat.DATA_URL).then(data=>{

            this.news = this.afDb.list('/news');
            this.news.update(item_key, {image1: data.downloadURL}); 


            });
        }


        if(this.capturedimage2 !== ''){

            let storageRef = firebase.storage().ref();

            const filename = 'img'+Math.floor(Date.now() / 1000);

            const imageRef = storageRef.child(`images/${filename}.jpg`);


            imageRef.putString(this.capturedimage2, firebase.storage.StringFormat.DATA_URL).then(data=>{

            this.news = this.afDb.list('/news');
            this.news.update(item_key, {image2: data.downloadURL}); 


            });
        }



        }).then(data=>{



            form.reset();
            this.capturedimage1 = '';
            this.capturedimage2 = '';
            this.navCtrl.parent.select(0);

        });





}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从 firebase 存储获取多个下载 URL 并将它们保存到 firebase firestore

如何将图像和文档上载到Firebase Storage,获取下载URL并将它们同时插入实时数据库?

如何从Firebase存储PHP获取图像的下载URL?

如何在Flutter中将多个图像上传到Firebase并获取其所有下载URL

通过传递Firebase存储参考获取图像下载URL

Unity- 将图像上传到 Firebase 存储后如何获取下载 URL?

如何获取 firebase 存储的下载 url 并更新 firestore 文档?

将图像成功上传到Firebase存储后,Firebase获取下载URL

成功将图像保存到Firebase云存储后,firebase函数获取下载URL

如何从Firebase存储到Firestore获取图像URL

如何将图像 URL 从 Firebase Storage 直接检索到图像视图?

如何获取Firebase存储下载URL的值并将其存储在Firebase存储中....,

获取Firebase存储文件的下载URL

从Firebase存储获取多个URL

异步上传多个文件并在 firebase 存储问题中获取它们的 url

如何将图像 URL(来自 Firebase 存储)存储到 Firestore [Flutter]

如何获取Firebase存储URL

如何将多个图像上传到 firebase 存储并将所有 url 存储在一个数组中到 firestore

无法获取上传到 firebase 存储中的图像的实际下载 URL

从上传的图像中获取下载 URL - Firebase 和 Swift

尝试从Firebase获取文件下载URL时出现Firebase错误

尝试将 url 段发布到 firebase

将文件上传到 Android 中的 Firebase 云存储后,我无法获取下载 URL

如何使用Firebase从上传的文件中获取下载URL

如何从“ addOnSuccessListener”内部的firebase存储中获取String中的下载URL

将Firebase存储下载URL推送到Firebase Cloud Firestore

如何将照片从android发布到云端并获取其url更改?

如何将多个文件图像加载为数据URL,然后分别更改它们

如何将 Url 从 Firebase Storage 存储到 ArrayList 中?