javascript - 当我将图像上传到 Firebase 存储时,如何制作进度条?

标签 javascript firebase vue.js firebase-storage

我想取得进展,向用户显示文件上传了多少,这样他就不会在上传过程中退出,任何人都可以帮助我如何在 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">

最佳答案

  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 ; 中,this函数进度的手段。

关于javascript - 当我将图像上传到 Firebase 存储时,如何制作进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55836764/

相关文章:

javascript - 将图像和json对象写入剪贴板

javascript - jQuery:将 img alt 标签更改为列表值

typescript - Vue.js、Typescript 和 VeeValidate 打字

javascript - AngularJS 在用户点击范围内等待 $resource Promise

firebase - 是否可以访问 Firebase 规则的 key ?

firebase - Firestore 离线数据 : Merging writes, 离线持久化的最大时间

java - fragment 事务出错(空对象引用)

javascript - Vue.js 路由器不工作

javascript - 使用 javascript 检测浏览器版本以实现 google swiffy 兼容性

php - 带有某些 html 标记的 JSON 字符串在解析时失败