按照https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html中的示例,我已经设置了带进度条的文件上传。
然而,由于我使用了<input type="file" multiple>
,所以上传需要通过以下循环完成:
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) {
});
}
问题是:如何将上传进度(请参见
var what
)分配给相应的文件?我尝试过的所有方法都没有成功:
The callback function
onUploadProgress
apparently doesn't take any second argument: https://github.com/mzabriskie/axios#request-configThe 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 whenonUploadProgress
gets called during the upload)I couldn't figure out a way to access axios'
data
from insideonUploadProgress
this
refers to:XMLHttpRequestUpload { onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, ontimeout: null, onloadend: null }
还有其他想法吗?