1,先在el-upload設置accept屬性,限定文件類型
// 上傳文件之前對文件大小和類型做限制,官方自帶accept屬性還是可以選中全部文件 beforeUpload(file) { let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/bmp', 'image/png']; const isImage = types.includes(file.type); const isLtSize = file.size / 1024 / 1024 < 2; if (!isImage) { this.$message.error('上傳圖片只能是 JPG、JPEG、gif、bmp、PNG 格式!'); return false; } if (!isLtSize) { this.$message.error('上傳圖片大小不能超過 2MB!'); return false; } return true; },