Vue-resource: Progress bar using xhr: onprogress

Created on 25 Feb 2016  路  5Comments  路  Source: pagekit/vue-resource

this.$http.post('/upload', formData, {
xhr: {
onprogress: (e) => {
this.progress = (e.position / e.totalSize)*100;
}
}
}).then...

The formData contains one image.

When I run the code, it always returns 100 at one time.

Most helpful comment

This worked for me

Vue.http.post('/api/upload', formData, {
  progress(e) {
    if (e.lengthComputable) {
      console.log("e.loaded: %o, e.total: %o, percent: %o", e.loaded, e.total, (e.loaded / e.total ) * 100);
    }
  }
})

All 5 comments

I figure out

this.$http.post('/upload', formData, {
upload: {
onprogress: (e) => {
if (e.lengthComputable) {
this.progress[this.count].loaded = e.loaded;
this.progress[this.count].total = e.total;
}
}
}
}).then...

I tried this, but I am still not seeing the callback being called.

```
this.$http.post('/api/upload', formData, {
headers: {'Content-Type': 'multipart/form-data'},
upload: {
onprogress: (e) => {
console.log(e)
}
}
}

Even using throttling, I am not able to use onprogress properly. Any idea?

This worked for me

Vue.http.post('/api/upload', formData, {
  progress(e) {
    if (e.lengthComputable) {
      console.log("e.loaded: %o, e.total: %o, percent: %o", e.loaded, e.total, (e.loaded / e.total ) * 100);
    }
  }
})

Thanks @abou7mied, worked great for me :tada:

Posting this amendment, for any future readers, in case anyone has issues with use of string substitution (%o) in console.log. This is currently only available in Firefox, so need to replace that with template literals or concatenation.

Example with template literals:

Vue.http.post('/api/upload', formData, {
  progress(e) {
    if (e.lengthComputable) {
      console.log(`e.loaded: ${e.loaded}, e.total: ${e.total}, percent: ${(e.loaded / e.total ) * 100}`);
    }
  }
})

Example with concatenation:

Vue.http.post('/api/upload', formData, {
  progress(e) {
    if (e.lengthComputable) {
      console.log('e.loaded:', e.loaded, 'e.total:', e.total, 'percent:', (e.loaded / e.total ) * 100);
    }
  }
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

V-Tom picture V-Tom  路  3Comments

okandavut picture okandavut  路  3Comments

briward picture briward  路  4Comments

yozman picture yozman  路  6Comments

Creabine picture Creabine  路  3Comments