2017-09-15 40 views
0
var someRandomURL = "";  
var task = storageRef.put(file); 
       task.on('state_changed', 
        function progress(snapshot) { 
        var percentageUploaded = (snapshot.bytesTransferred/snapshot.totalBytes) * 100; 
        // uploader.value = percentageUploaded; 
        }, 
        function error(err) { 
        console.log(err); 
        }, 
        function complete() { 
        console.log("Upload completed successfully!"); 
        somRandomURL = task.snapshot.downloadURL; 
        } 
      ); 

這是我所做的捕獲我已上傳的圖像的downloadURL,它正在上傳正確,但我無法獲取downloadURL。我可以知道我要去哪裏嗎?或者我濫用downloadURL屬性?我是否正確抓取downloadURL?

回答

0
var percentageUploaded = (snapshot.bytesTransferred/snapshot.totalBytes) * 100; //check if its' 100% then get the download url like below, 
if(percentageUploaded ==100){ 
    storageRef.getDownloadURL().then(function(url) { 
     someRandomURL =url; //here you'll get the download url once the file is uploaded. 
    }) 
} 

看到這個Stackoverflow answer for Getting download url.

Getting Started with Firebase Storage on the Web - Firecasts by firebase channel from youtube。我希望這可以幫助你。

增加了JSfiddle sample

+0

我試過了,但它似乎沒有工作。 –

+0

等一下,我會爲此添加一個jsfiddle示例。 – MuruGan

+0

是的,在此之前,我將向您展示storageRef的代碼,我想這就是我要出錯的地方。 –