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;
       }
     },        

 


免責聲明!

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



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