从多个文件上传Firebase存储中获取下载URL [英] get download url from multiple file upload firebase storage

查看:96
本文介绍了从多个文件上传Firebase存储中获取下载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase和angularjs的新手,我很难从firebase的存储中获取下载URL并将其存储在firebase实时数据库中.

I am new in firebase and angularjs and i am having difficulties in getting download url from firebase storage and store them in firebase realtime database.

我能够将多个文件上传到Firebase存储.问题是当我将下载URL存储到Firebase实时数据库中时,所有数据库URL值都相同.每个文件的downloadURL应该不同.

I was able to upload multiple files to firebase storage. the problem is when i store the download url into firebase realtime database, all database url value are same.It should different based each files downloadURL.

这是我的脚本:

$scope.submitPhotos = function(file){
  console.log(file);
  var updateAlbum = [];
  for (var i = 0; i < file.length; i++) {                
    var storageRef=firebase.storage().ref(albumtitle).child(file[i].name);
    var task=storageRef.put(file[i]);

    task.on('state_changed', function progress(snapshot){
      var percentage=( snapshot.bytesTransferred / snapshot.totalBytes )*100;
      if (percentage==100){
      storageRef.getDownloadURL().then(function(url) {
        var galleryRef = firebase.database().ref('gallery/'+albumkey);
        var postkey = firebase.database().ref('gallery/'+albumkey).push().key;       
        updateAlbum={img:url};
        firebase.database().ref('gallery/'+ albumkey+'/'+postkey).update(updateAlbum);
      });
    };
  })  
  };
};

如您所见,我能够将URL存储到数据库中,但是所有URL都是相同的.我需要的是每个密钥存储区中每个与存储区不同的链接.

As you can see i was able store the url into database but all of the urls are same. What i need is every key store each different links from storage.

任何帮助都值得赞赏.谢谢

Any helps appreciated. Thanks

推荐答案

function uploadImg(file,i) {

return new Promise((resolve,reject)=>{


var storageRef=firebase.storage().ref("store-images/"+file[i].file.name);
             task = storageRef.put(file[i].file);

            task.on('state_changed', function progress(snapshot){

            var percentage=( snapshot.bytesTransferred / snapshot.totalBytes )*100;
            console.log(percentage);

            // use the percentage as you wish, to show progress of an upload for example
            }, // use the function below for error handling
            function (error) {  
              console.log(error);
            }, 
            function complete ()  //This function executes after a successful upload
            {

                    task.snapshot.ref.getDownloadURL().then(function(downloadURL) {

                        resolve(downloadURL)

                    });


            });  

    })

}


async function putImage(file) {

    for (var i = 0; i < file.length; i++) {

            var dd = await uploadImg(file,i);

            firebase.database().ref().child('gallery').push(dd);
        }

    }

这篇关于从多个文件上传Firebase存储中获取下载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆