el-upload上传组件(隐藏上传按钮/隐藏文件删除标记)

<template>
  <el-upload
    ref="uploadFile"
    :class="[showUploadBtn ? '' : 'hidden-Btn']"   //是否隐藏点击上传的按钮
    :disabled="!showUploadBtn"                     //是否隐藏文件删除标记
    action="#none"
    :http-request="uploadSectionFile"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :on-change="fileChange"
    :auto-upload="false"
    multiple
    :on-exceed="handleExceed"
    :file-list="fileList"
  >
    <el-button size="small" type="primary">点击上传</el-button>
  </el-upload>
</template>

<script>
export default {
  name: "UploadFiles",
  props: {
    files: {
      type: Array,
      default: () => [],
    },
    /*是否显示上传文件按钮、是否显示文件删除×号*/
    showUploadBtn: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      fileList: this.files,
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
      // 改变文件列表
      this.fileList = fileList;
    },
    handlePreview(file) {
      console.log("handlePreview:", file);
      if (file.url) {
        window.open(file.url);
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    // param是自带参数。
    // this.$refs.uploadFile.submit()会自动调用 httpRequest方法.在里面取得file
    uploadSectionFile(param) {
      var fileObj = param.file;
      // FormData 对象
      var form = new FormData();
      // 文件对象
      form.append("file", fileObj);
      console.log("dfdg:");
      console.log(form);
      let arr = [];
      this.fileList.forEach((element) => {
        let formData = new FormData();
        formData.append("file", element);
        arr.push(formData);
      });
    },
    // fileList 是文件列表发生变化后,返回的修改后的列表对象,这里直接赋值
    fileChange(file, fileList) {
      console.log(file.raw);
      // 改变文件列表
      this.fileList = fileList;
    },
  },
};
</script>

<style scoped lang="scss">
.hidden-Btn {
  /deep/ .el-upload {
    display: none;
  }
}
</style>

 

posted @ 2021-09-17 16:22  一两米  阅读(5039)  评论(0编辑  收藏  举报