2017-02-18 75 views
1

我是firebase和angularjs中的新成員,我很難從firebase存儲獲取下載url並將它們存儲在firebase實時數據庫中。從多個文件上傳Firebase存儲獲取下載url

我能夠上傳多個文件到firebase存儲。問題是當我將下載url存儲到firebase實時數據庫中時,所有數據庫的url值都是相同的。它應該根據每個文件的不同而有所不同。

這裏我的腳本:

$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); 
     }); 
    }; 
    }) 
    }; 
}; 

enter image description here

正如你可以看到我能夠店的網址到數據庫中,但所有的URL都是一樣的。我需要的是每個關鍵存儲每個不同的存儲鏈接。

任何幫助讚賞。由於

回答

0

嘗試使用下面的代碼:

$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; 
     // use the percentage as you wish, to show progress of an upload for example 
    }, // use the function below for error handling 
    function (error) { 

      switch (error.code) { 
      case 'storage/unauthorized': 
       // User doesn't have permission to access the object 
       break; 

      case 'storage/canceled': 
       // User canceled the upload 
       break; 

      case 'storage/unknown': 
       // Unknown error occurred, inspect error.serverResponse 
       break; 
      } 

     }, function complete() //This function executes after a successful upload 
     { 
     let dwnURL = task.snapshot.downloadURL; 
     let galleryRef = firebase.database().ref('gallery/'+albumkey); 
     let postkey = firebase.database().ref('gallery/'+albumkey).push().key;  
     updateAlbum={img:dwnURL}; 
     firebase.database().ref('gallery/'+ albumkey+'/'+postkey).update(updateAlbum); 
     }); 


    }; 
}; 

所有最優秀的!

+0

它仍然不起作用。我使用代碼獲得的結果是firebase數據庫僅存儲上載到Firebase存儲的第一個圖像的網址。我需要我的文件存儲在firebase數據庫中的所有下載url –

+0

嗨,我已經對上面的代碼做了一些代碼更改;刪除了幾行,根據需要進行調整。你能確認'文件'數組包含提交多個文件嗎? –