2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

Add 'progress' config option for monitoring progress events for uploads and downloads.

This commit is contained in:
Davis Barber
2015-07-30 13:54:53 -04:00
parent 8c1694e821
commit 261e41644d
3 changed files with 28 additions and 7 deletions
+7 -1
View File
@@ -186,7 +186,13 @@ This is the available config options for making requests. Only the `url` is requ
xsrfCookieName: 'XSRF-TOKEN', // default xsrfCookieName: 'XSRF-TOKEN', // default
// `xsrfHeaderName` is the name of the http header that carries the xsrf token value // `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
}
} }
``` ```
+7 -2
View File
@@ -26,7 +26,13 @@
data.append('foo', 'bar'); data.append('foo', 'bar');
data.append('file', document.getElementById('file').files[0]); 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) { .then(function (res) {
output.className = 'container'; output.className = 'container';
output.innerHTML = res.data; output.innerHTML = res.data;
@@ -40,4 +46,3 @@
</script> </script>
</body> </body>
</html> </html>
+10
View File
@@ -11,6 +11,7 @@ var transformData = require('./../helpers/transformData');
var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin'); var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin');
module.exports = function xhrAdapter(resolve, reject, config) { module.exports = function xhrAdapter(resolve, reject, config) {
// Transform request data // Transform request data
var data = transformData( var data = transformData(
config.data, 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)) { if (utils.isArrayBuffer(data)) {
data = new DataView(data); data = new DataView(data);
} }