使用saga和axios创建上传进度

问题描述

我正在尝试显示我的带有react.problem的上传进度。问题是eventchannel不会发出百分比。watchOnProgress内部的数据为null。文件正确上传,但我无法显示百分比

saga.js:

const identity = a => a;

const createAsyncUpload = file => {
  let emit;
  const chan = eventChannel(emitter => {
    emit = emitter;
    return () => {};
  });

  const promise = uploadVideoApi(file,function(e) {
    emit((e.loaded * 100) / e.total);
  })
    .then(response => emit({ state: 'ok',response }))
    .catch(error => emit({ state: 'nok',error }));

  return [promise,chan];
};

function* watchOnProgress(chan) {
  while (true) {
    const data = yield take(chan);
    if (typeof data === 'number') {
      yield put(uploadFileProgress(data));
    } else if (data.state === 'ok') {
      console.log('hi');
      yield put(uploadFileSuccess(data.response.data));
    } else {
      yield put(uploadFileFail(data.error.response));
    }
  }
}

function* uploadVideo({ file }) {
  try {
    const [promise,chan] = createAsyncUpload(file);
    yield fork(watchOnProgress,chan);
    yield call(identity(promise));
  } catch (error) {
    yield put(uploadFileFail(error));
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)