将多个文件上传到Firebase存储

时间:2019-03-24 20:45:52

标签: javascript firebase firebase-storage

我正在开发一个以firebase为后端的烹饪android应用程序,我需要在firebase存储中上传食谱的多个图像,然后将downloadurl存储到firebase数据库中

我使用了https://stackoverflow.com/a/46521788/2189409中的这段代码 并且在firebase存储中上传多个文件也很好,但是我不知道如何获取这些文件的downloadUrl,请提供任何帮助。  这是我这部分的Java脚本代码:

firebase.storage().ref().constructor.prototype.putFiles = 
function(recipeImages) { 
        var ref = this;
    return Promise.all(Array.from(recipeImages).map(function(file) {
        return ref.child(file.name).put(file);
    }));
    }
 firebase.storage().ref(fullDirectory+"/"+imageFile.name);

        var category=$("#category").val();
        var cook=$("#cook").val();
        var name=recipeimages["name"];
        var ext= name.substring(name.lastIndexOf("."),name.length);
        var receipename = new Date().getTime();
        var uploadTime = new Date().getTime();
        var storageRef = firebase.storage().ref(receipes+"/"+title+"/"+uploadTime+recipeImages.name );
        var  imagesUrl;          
        firebase.storage().ref("receipes"+"/"+category+"/"+title ).putFiles(recipeImages).then(function(metadatas) {
            // Get an array of file metadata

            imagesUrl=0;
            alert("this is meta:", metadatas);

            alert("la taille est de :",metadatas.length);
            imagesUrl=metadatas.fullPath;
            alert(imagesUrl);

            var category=$("#category").val();         
            var cook=$("#cook").val();
            var ext= name.substring(name.lastIndexOf("."),name.length);
            var uploadTime = new Date().getTime();

firebase.storage().ref(receipes+"/"+title+"/"+uploadTime+recipeImages.name );
            var database = 
            firebase.database().ref("receipes").child(category);
            var receipeid = database.push().key;

                var receipe= 
                {
                    "title":title,
                    "cook":cook,
                    "imagesUrl": imagesUrl,
                    "title":title,
                    "desc":desc,
                    "preparationTime":preparationTime,
                    "ingr":ingr,
                    "cookTime":cookTime, 
                    "portion":portion ,
                    "nbrImage": nbrImage,
                };
                database.child(title+"_"+cook).set(receipe,function(err)
                {
                    if(err){
                            $("#result").attr("class", "alert alert-danger");
                            $("#result").html(err.message);
                        }else{
                            $("#result").attr("class", "alert alert-success");
                            $("#result").html("receipe added");
                        }
                        resetForm();
                });
            }).catch(function(error) {

             });

0 个答案:

没有答案