将图像上传到Firebase存储时如何制作进度栏?

时间:2019-04-24 18:51:48

标签: javascript firebase vue.js firebase-storage

我想取得进展以向用户显示文件已上传了多少,这样他就不会退出n在上传过程中有人可以帮助我如何在vue js中制作进度条

我正在使用此firebase代码显示百分比,我已经使用v-model和p标签显示了百分比,但是没有用

方法:{

uploadfiles(e) {
  var storage = firebase.storage();
  var user = firebase.auth().currentUser;
  var file = e.target.files[0];
  var path = "users/" + user.uid + "/attachments/" + Date.now() + ".jpg";
  var storageRef = storage.ref(path);
  var task = storageRef.put(file);
      task.on('state_changed');
    function progress (self, snapshot){
      var percentage = (snapshot.bytesTransferred/snapshot.totalBytes)*100;
        console.log('this is image =',percentage);
          self.progress = percentage;
           console.log(progress);
    }

    task.then(snapshot => {
      console.log("image uploaded successfully");
      //saveMessageInDatabase(newmessagedata);
      return task.snapshot.ref.getDownloadURL();
    })
    .then(url => {
      console.log("download url of image", url);
      this.username.image = url;
      console.log(this.username.image);
    })
    .catch(error => {
      // hideMessageDialog();
      console.log("image uploaded error");
    });
}

} HTML

  <p>{{this.progress}}%</p>

   <progress max="100"  :style="{width: progress+'%'}" value="0"> 
 </progress> 

<div class="progress-bar" role="progressbar" v-bind:aria-valuenow="this.progress" aria-valuemin="0" aria-valuemax="100" :style="{width: this.progress+'%'}"></div>

    <input id="pic" type="file"  @change="uploadfiles">

1 个答案:

答案 0 :(得分:1)

  function progress (self, snapshot){
      var percentage = (snapshot.bytesTransferred/snapshot.totalBytes)*100;
      console.log('this is image =',percentage);
      self.progress = percentage ;
    console.log(progress);
  }

this解析为progress,将self用作功能。
this.progress = percentage ;中,thisfunction progress的意思。