赞
踩
<el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :auto-upload="false" ref='uploadPhoto' :multiple="false" :on-preview="handlePictureCardPreview" :before-upload="beforeUploadImg" :on-change="changeUploadImg" :on-remove="handleRemove" @clearFiles="clearFilesPhoto" accept="image/jpeg,image/png"> <i class="el-icon-plus"></i> </el-upload> <el-button @click="cancelUploadPhoto">取 消</el-button> <el-button type="primary" @click="submitUpload">发送</el-button>
//文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 changeUploadImg(file) { console.log(file); //保存消息图片 // this.messageImgPost = file.raw; const isJPG = file.raw.type === 'image/jpeg'; const isPNG = file.raw.type === 'image/png'; // const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG) { this.$message.error('上传图片只能是 JPG 或者 PNG 格式!'); //执行清空图片 // this.$refs.uploadPhoto.clearFiles(); // 取消时在文件列表中删除该文件 this.$refs.uploadPhoto.handleRemove(file); return (isJPG || isPNG); } else { this.newArrayImg.push(file.raw); this.messageImgPost = this.newArrayImg; } },
注意:这里使用了 this.$refs.uploadPhoto.handleRemove(file);清空非JPG、PNG文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。