elementui上傳圖片限制上傳類型


elementui上傳圖片限制上傳類型

// 封裝一個判斷圖片文件后綴名的方法
function isImage(fileName) {
  if (typeof fileName !== 'string') return;
  let name = fileName.toLowerCase();
  return name.endsWith('.png') || name.endsWith('.jpeg') || name.endsWith('.jpg') || name.endsWith('.png') || name.endsWith('.bmp');
}

 

 

 

 我們看到有個屬性是:on-change=uploadChange,在每一次點擊上傳按鈕,上傳列表發生改變時,我們在此時做個處理如下

     // 方法判斷
     uploadChange(file, fileList) {
        // 拿到當前上傳圖片的name,判斷其后綴名是否符合
        let type = isImage(file.name);
        console.log(type);
        this.fileList = fileList;
        let fileLength = this.fileList.length;
        if (!type) {
         // 若不符合圖片類型,則讓當前上傳的文件去除掉即可,即從上傳對列刪除本次上傳
          this.fileList.splice(fileLength - 1, 1);
          this.$message.error('只允許上傳圖片');
        }
        this.isHasDataChange = true;

      },

往前端的道路一去不復返,兄弟姐妹們加油,原諒的顏色


免責聲明!

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



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