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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM