我正确地抓住了downloadURL吗?

时间:2017-09-15 01:33:20

标签: javascript firebase firebase-storage

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属性?

1 个答案:

答案 0 :(得分: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