el-upload一次请求,上传多份文件

288 阅读1分钟
<el-upload
      ref="uploadForm"
      class="upload-demo"
      drag
      :action="action"
      :show-file-list="true"
      multiple
      :auto-upload="false"
      :file-list="fileList"
      :on-change="onChange"
      :on-remove="onRemove"
    >
      <i class="el-icon-upload" v-loading="isLoading"></i>
      <div class="el-upload__text">drag file to here,or<em> click upload</em></div>
    </el-upload>
    <el-row slot="footer">
      <el-button type="primary" @click="uploadConfirm" :disabled="fileList.length == 0" :loading="isConfirmLoading">Confirm</el-button>
    </el-row>

上传多个文件,多个文件统一提交,必须要把auto-upload设为false,此属性是禁止单条文件选择后,立即上传。

script部分

<script>
import TapeService from '@/service/TapeService';
import webConfig from '@/config/config';
import AuthService from '@/core/AuthService';
export default {
  data() {
    return {
      isShowDialog: false,
      token: '',
      isLoading: false,
      isConfirmLoading: false,
      fileList: [],
      fileData: ''
    };
  },
  methods: {
    // 选择文件,赋值给定义好的fileList
    onChange(file, fileList) {
      this.fileList = fileList
    },
    // 删除文件操作
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    uploadConfirm() {
      const fd = new FormData()
      this.fileList.forEach(item => {
        fd.append('files', item.raw)
      })
      this.isLoading = true
      this.isConfirmLoading = true
      TapeService.upload(1, fd).then(res => {
        console.log('upload--res---', res)
        if (res) {
          if (res) {
            this.$message({
              type: 'success',
              message: 'upload success'
            })
            this.isLoading = false
            this.isConfirmLoading = false
            // this.isShowDialog = false
            this.$refs.uploadForm.onSuccess()
          }
        }
      }).finally(() => {
        this.isConfirmLoading = false
        this.isLoading = false
      })
    }
  },
  async mounted() {
    const auth = await AuthService.getUser();
    this.token = auth.access_token;
    // console.log('token---', this.token)
  },
  computed: {
    action() {
      return `${webConfig.dmapi.url}/tape/upload-files?urlWatcherId=1`;
    },
    headers() {
      return {
        authorization: `Bearer ${this.token}`
      }
    }
  }
};
</script>

参数.png