Vue使用 Element 組件實現文件一次性批量上傳


Vue使用 Element 組件實現文件一次性批量上傳

HTML

<el-upload class="upload-demo" ref="upload" action="https://jsonplaceholder.typicode.com/posts/" :file-list="fileList" :auto-upload="false" :headers="{token: $cookie.get('token')}" :multiple='true' :show-file-list='true' :on-change="handleChange" :on-remove="handleRemove">
   <el-button slot="trigger" size="small" type="primary" icon="el-icon-plus" plain>模板文件選擇,已選擇{{fileList.length}}個</el-button>
   <el-button style="margin-left: 15px;" size="small" type="success" @click="submitUpload" :disabled="fileList.length<=0" icon="el-icon-upload" plain>將選擇文件上傳服務器</el-button>
</el-upload>

JavaScript

export default {
	data() {
      return {
        fileList: [], // 上傳文件列表
        uploading: false,  // 是否正在上傳文件
      }
    },
    methods: {
      // 選擇文件改變
      handleChange(files, fileList) {
        this.fileList = fileList
      },
      // 移除文件
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      // 上傳文件
      submitUpload() {
        console.log('---> ', this.fileList)
        this.uploading = true
        setTimeout(() => {
          this.uploading = false
          this.fileList = []
          this.$message({
            message: '文件上傳成功!',
            type: 'success'
          });
        }, 5000)
      },
   }
}

CSS

 .l-s {
    background-color: #fff;
    padding: 20px;
    padding-top: 1px;
  }

  .d-z {
    text-align: center;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(255, 255, 255, 0.5);
    z-index: 1000;
  }

最終效果


免責聲明!

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



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