【问题标题】:How to send 1 file at a time? Wait then Send Next - Mobx / Axios如何一次发送1个文件?等待然后发送下一个 - Mobx / Axios
【发布时间】:2018-10-06 04:05:01
【问题描述】:

我有用户上传的多张图片,一旦他们点击上传按钮,我想开始上传每张图片,我首先喜欢一次开始一张,也许一次尝试一批 5 张。

  @action
   uploadAllFiles = async () => {
    for (let i = 0; i < this.files.length; i++) {
         runInAction(async () => {
            const file = this.files[i];
            file.uploading = true;
             var data = new FormData();
            data.append('folderName', '4141515');
            data.append('file', file.fileObject);
            await axiosGenericInstance.post('/Images', data);
            this.files.remove(file);
         });
    }
  };

但我明白了

由于启用了严格模式,因此更改观察到的可观察值 不允许外部行为。请将代码包装在action if 这种变化是有意的。尝试修改:

所以我做的不对。

【问题讨论】:

  • runInAction 函数有什么作用?

标签: javascript reactjs async-await axios mobx


【解决方案1】:

我是这样做的:

export function getDetails(n, dataArray) {
return async(dispatch) => {
  for(let i = 0; i < n; i++){
      myService.getDetails(dataArray['x'][i]['y'])
        .then(res => {
          let item = dataArray['x'][i]['y']
          dispatch({type: types.DETAILS_GRAPH, item, i, res});
          })
        .catch(error => {
          dispatch({type: types.SET_ERROR, error});      
        });
  }
 //sleep(1) here if time needed between 2 calls
 };
}

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我认为你应该awaitrunInAction

    for (let i = 0; i < this.files.length; i++) {
        // first write await      
        await  runInAction(async () => {
            const file = this.files[i];
            file.uploading = true;
            var data = new FormData();
            data.append('folderName', '4141515');
            data.append('file', file.fileObject);
            await axiosGenericInstance.post('/Images', data);
            this.files.remove(file);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2014-10-04
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多