是否有干净/简短/正确的方法来一起使用 axios Promise和上传进度事件?
假设我有下一个上传功能:
function upload(payload, onProgress) {
const url = '/sources/upload';
const data = new FormData();
data.append('source', payload.file, payload.file.name);
const config = {
onUploadProgress: onProgress,
withCredentials: true
};
return axios.post(url, data, config);
}
这个函数返回了Promise。
我还有一个传奇:
function* uploadSaga(action) {
try {
const response = yield call(upload, payload, [?? anyProgressFunction ??]);
yield put({ type: UPLOADING_SUCCESS, payload: response });
} catch (err) {
yield put({ type: UPLOADING_FAIL, payload: err });
}
}
我想接收进度事件并将其放在 saga 中。我还想捕获 axios 请求的成功(或失败)结果。是否可以?
谢谢。