From 261e41644dcb4c3c4dcc4b695635b8f7b447fe70 Mon Sep 17 00:00:00 2001 From: Davis Barber Date: Thu, 30 Jul 2015 13:54:53 -0400 Subject: [PATCH] Add 'progress' config option for monitoring progress events for uploads and downloads. --- README.md | 16 +++++++++++----- examples/upload/index.html | 9 +++++++-- lib/adapters/xhr.js | 10 ++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5cba5ed5..5be5d921 100644 --- a/README.md +++ b/README.md @@ -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: { @@ -147,7 +147,7 @@ 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; }], @@ -155,7 +155,7 @@ This is the available config options for making requests. Only the `url` is requ // it is passed to then/catch transformResponse: [function (data) { // Do whatever you want to transform the data - + return data; }], @@ -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 + } } ``` @@ -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', diff --git a/examples/upload/index.html b/examples/upload/index.html index 5841a1c6..57125d5a 100644 --- a/examples/upload/index.html +++ b/examples/upload/index.html @@ -26,7 +26,13 @@ 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; @@ -40,4 +46,3 @@ - diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index dc5c6d03..1068773e 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -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, @@ -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); }