Reputation: 8355
Following https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html I've set up a file upload with progress bar.
However, I have <input type="file" multiple>
, so the upload is inside a loop like this:
for (var i=0; i<files.length; i++)
{
var config = {
onUploadProgress: function(progressEvent) {
var what = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
}
};
axios.post(url, data, config)
.then(function (response) {
});
}
The question is: How can I assign the upload progress (see var what
) to the corresponding file?
Everything I've tried didn't work:
The callback function onUploadProgress
apparently doesn't take any second argument: https://github.com/mzabriskie/axios#request-config
The injected progressEvent
object doesn't contain any information about the uploaded file. Example:
progress { target: XMLHttpRequestUpload, isTrusted: true, lengthComputable: true, loaded: 181914, total: 181914, currentTarget: XMLHttpRequestUpload, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, composed: false }
The looping variable i
is accessible in principle - however, it's always at the last position (since the loop has finished when onUploadProgress
gets called during the upload)
I couldn't figure out a way to access axios' data
from inside onUploadProgress
this
refers to:
XMLHttpRequestUpload { onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, ontimeout: null, onloadend: null }
Any other ideas?
Upvotes: 4
Views: 12314
Reputation: 834
You can create a function that return another decorated function with some Id as parameter.
Example:
const myUploadProgress = (myFileId) => (progress) => {
let percentage = Math.floor((progress.loaded * 100) / progress.total)
console.log(myFileId)
console.log(percentage)
}
for (var i=0; i<files.length; i++) {
var config = {
onUploadProgress: myUploadProgress(files[i].id)
};
axios.post(url, data, config).then(function (response) {});
}
If you don't have ES6 you can do:
function myUploadProgress(myFileId) {
return function(progress) {
...
}
}
(I'm using a similar code on my project and it works like a charm)
Upvotes: 10
Reputation: 50115
This is a variable scoping issue. In JavaScript var
declares a variable in a function scope. There are a lot of potential solution, depending on which version of ES your target browser supports. I'll use the same function to demonstrate:
setTimeout(function(){ console.log(i); }, i * 100);
Using var
this will print out '5' 5 times. We want it to print out 0 1 2 3 4
.
If you have Array#forEach
, you can use that since the closure creates a new function scope
[0, 1, 2, 3, 4].forEach(function(i) {
setTimeout(function(){ console.log(i); }, i * 100);
});
If you have access to let
, let
and const
uses block scoping instead (ie. the variable is scoped to the nearest set of { ... }
).
for (let i = 0; i < 5; i++) {
setTimeout(function(){ console.log(i); }), i * 100);
}
If you don't have access to either of those, you can use an IIFE. It's really ugly but it gets the job done.
for (var i = 0; i < 5; i++){
(function() {
var newI = i;
setTimeout(function(){ console.log(newI); }, i * 100);
})();
}
Upvotes: 0