正在關注https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html我已經用進度條設置了文件上傳。Axios:上傳多個文件上傳的進度
不過,我有<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
)到相應文件?
一切我已經試過沒有工作:
回調函數
onUploadProgress
顯然不會採取任何第二個參數:https://github.com/mzabriskie/axios#request-config注入
progressEvent
對象不包含任何關於上傳文件的信息。例如:progress { target: XMLHttpRequestUpload, isTrusted: true, lengthComputable: true, loaded: 181914, total: 181914, currentTarget: XMLHttpRequestUpload, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, composed: false }
的循環變量
i
在原則上是可訪問的 - 然而,它在最後位置總是(因爲當onUploadProgress
被稱爲上載過程中循環完成)我不能'T想出一個辦法來訪問Axios公司' 從內部
onUploadProgress
this
指data
:XMLHttpRequestUpload { onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, ontimeout: null, onloadend: null }
還有其他想法嗎?
我在這裏錯過了什麼嗎?您位於for循環內並一次上傳一個文件。這意味着在每個progressEvent中,您已經指向相應的文件。你可以添加一個監聽器到progressEvent並打印出它的「總計」值嗎?我想你已經在看相應的文件了。作爲一個側面說明,將配置從for循環中取出,您不需要爲每個文件重新定義它。 –
是的,謝謝,我已經認爲答案可能是沒有答案;-)我只能在幾天內嘗試這個 - 我會報告然後... –
我也想知道如何做到這一點。使用jQuery,我一直使用「xhrFields」參數,但我不確定Axios是否有類似的東西。 – dvyio