formData实现文件上传

 <a-upload
          name="file"
          :file-list="fileList"
          :multiple="false"
          @change="changeFile"
          :custom-request="upLoadFileFn"
        >
          <a-button type="link">
            <a-icon type="cloud-upload" />
            {{ locale.upLoad }}
          </a-button>
        </a-upload>
通过custom-request自定义来覆盖组件的上传

 

 

 
async upLoadFileFn(data) {
    // data是组件传过来的文件
      const formData = new FormData()
      formData.append('fileList', data.file)
      formData.append('type', this.type)
      formData.append('objectId', this.id)
      // 上传提交
      const res = await upLoadFile(formData)
      console.log(res)
      if (res.code === 'SUCCESS') {
        // 调用组件内方法, 设置为成功状态
        this.$message.success('上传成功')
        this.dataSource = [...this.dataSource, ...res.data]
        console.log(data, 'woshidata')
        data.status = 'done'
      } else {
        data.onError()
        data.status = 'error'
      }
    },

 

 

posted @ 2022-04-15 11:14  MonsterSATSUKI  阅读(581)  评论(0)    收藏  举报