element ui 拖拽上传 on-change不触发

 1 <el-upload
 2     class="upload-demo"
 3     drag
 4     multiple
 5     action=""
 6     :file-list = "fileList"
 7     accept="application/vnd.ms-excel,application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
 8     :on-change="(file, fileList) => {handleChange(file, fileList)}"
 9     :on-remove="(file) => {handleRemove(file)}"
10     :before-upload="handleBeforeUpload"
11     :auto-upload="false">
12     <em class="el-icon-upload"></em>
13     <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
14     <div class="el-upload__tip" slot="tip">只能上传xlsx/xls/pdf文件,且不超过3m</div>
15 </el-upload>

element - ui启用拖拽功能后,on-change事件不触发。

 

解决方法:

去掉类型限制 accept = "application/vnd.ms-excel,application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 改为在on-change回调里面验证

 handleChange(file, fileList){
       console.log('handleChange函数被触发');
       // 判断file是否是新增
       let isExist = false;
       isExist = this.fileList.some(item => {
         if(item.name == file.name) 
           return true;
       });
       if(!isExist) {
         // 检测文件类型
         let fileType = file.name.substring(file.name.lastIndexOf('.')+1).toLowerCase();
         let isFileTypeReady = fileType === 'xlsx' || fileType === 'xls' || fileType === 'pdf';
         if(!isFileTypeReady) {
           // 文件类型不符合
           fileList.some((item, index) => {
             if(item.uid == file.uid) {
               fileList.splice(index, 1);
               return true;
             }
           })
           this.$message.error("上传文件只能是xlsx/xls/pdf格式");
           return false;
         } else {
           // 其他校验
         }
       } else {
         // 如果已经存在
         fileList.some((item, index) => {
           if(item.uid == file.uid) {
             fileList.splice(index, 1);
             return true;
           }
         });
         this.$message.error(`已经存在文件${file.name}, 请勿重复上传`);
         return false;
       }
     },        

 

posted @ 2020-09-10 14:11  一口一个小馒头  阅读(3582)  评论(0编辑  收藏  举报