Skip to content

Commit

Permalink
Add 'progress' config option for monitoring progress events for uploa…
Browse files Browse the repository at this point in the history
…ds and downloads.
  • Loading branch information
Davis Barber committed Aug 26, 2015
1 parent 8c1694e commit 261e416
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ axios.get('/user?ID=12345')
.catch(function (response) {
console.log(response);
});

// Optionally the request above could also be done as
axios.get('/user', {
params: {
Expand Down Expand Up @@ -147,15 +147,15 @@ This is the available config options for making requests. Only the `url` is requ
// The last function in the array must return a string or an ArrayBuffer
transformRequest: [function (data) {
// Do whatever you want to transform the data

return data;
}],

// `transformResponse` allows changes to the response data to be made before
// it is passed to then/catch
transformResponse: [function (data) {
// Do whatever you want to transform the data

return data;
}],

Expand Down Expand Up @@ -186,7 +186,13 @@ This is the available config options for making requests. Only the `url` is requ
xsrfCookieName: 'XSRF-TOKEN', // default

// `xsrfHeaderName` is the name of the http header that carries the xsrf token value
xsrfHeaderName: 'X-XSRF-TOKEN' // default
xsrfHeaderName: 'X-XSRF-TOKEN', // default

// `progress` allows handling of progress events for 'POST' and 'PUT uploads'
// as well as 'GET' downloads
progress: function(progressEvent) {
// Do whatever you want with the native progress event
}
}
```

Expand All @@ -201,7 +207,7 @@ The response for a request contains the following information.

// `status` is the HTTP status code from the server response
status: 200,

// `statusText` is the HTTP status message from the server response
statusText: 'OK',

Expand Down
9 changes: 7 additions & 2 deletions examples/upload/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ <h1>file upload</h1>
data.append('foo', 'bar');
data.append('file', document.getElementById('file').files[0]);

axios.put('/upload/server', data)
var config = {
progress: function(progressEvent) {
var percentCompleted = progressEvent.loaded / progressEvent.total;
}
};

axios.put('/upload/server', data, config)
.then(function (res) {
output.className = 'container';
output.innerHTML = res.data;
Expand All @@ -40,4 +46,3 @@ <h1>file upload</h1>
</script>
</body>
</html>

10 changes: 10 additions & 0 deletions lib/adapters/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var transformData = require('./../helpers/transformData');
var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin');

module.exports = function xhrAdapter(resolve, reject, config) {

// Transform request data
var data = transformData(
config.data,
Expand Down Expand Up @@ -100,6 +101,15 @@ module.exports = function xhrAdapter(resolve, reject, config) {
}
}

// Handle progress if needed
if (config.progress) {
if (config.method === 'post' || config.method === 'put') {
request.upload.addEventListener('progress', config.progress);
} else if (config.method === 'get') {
request.addEventListener('progress', config.progress);
}
}

if (utils.isArrayBuffer(data)) {
data = new DataView(data);
}
Expand Down

0 comments on commit 261e416

Please sign in to comment.