iview中如何实现,点击按钮后开始上传附件

1.使用iview中的upload组件,选择好文件后,点击“上传”按钮才开始上传
2.iview的upload api文档有说明,

before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传

所以我的想法是,通过before-upload回调函数中返回一个promise对象,点击上传后,执行promise的resolve方法,这样就可以实现点击后上传

目前的问题是,点击“上传”按钮,如何执行Promise的resolve方法,vue中如何实现

upload(rawFile, file) {
  this.$refs.input.value = null;
  if (!this.beforeUpload) {
    return this.post(rawFile);
  }
  const before = this.beforeUpload(rawFile);
  if (before && before.then) {
    before.then(processedFile => {
      if (Object.prototype.toString.call(processedFile) === '[object File]') {
        this.post(processedFile);
      } else {
        this.post(rawFile);
      }
    }, () => {
      this.onRemove(null, rawFile);
    });
  } else if (before !== false) {
    this.post(rawFile);
  } else {
    this.onRemove(null, rawFile);
  }
},
阅读 6.5k
2 个回答
新手上路,请多包涵

你可以把返回的promise存储到一个data里把, 再通过click参数传递给按钮点击事件。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题