上传图片
- on-preview 点击文件列表中已上传的文件时的钩子(点击弹出框预览图片)
- on-remove 清空已上传的文件列表
- before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
- on-change 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
<el-form-item label="上传图片" ref="uploadelRef" prop="logo"> <el-upload action="" ref="uploadel" list-type="picture-card" :auto-upload="false" :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :limit='1' :before-upload="beforeImgUpload" :on-change="loadJsonFromFile" :file-list="loadupload" > <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus"></i> </el-upload> </el-form-item><el-dialog title="图片预览" :modal="false" :visible.sync="dialogVisibleimg"> <img width="100%" :src="dialogImageUrl" alt=""></el-dialog>
<script> export default { data(){ return{ loadupload: [],//文件列表 } }, methods:{ //点击文件列表中已上传的文件时的 handlePictureCardPreview(file) { this.formData.handlePic = file.url; this.dialogImageUrl = file.url; this.dialogVisibleimg = true; }, //清空已上传的文件列表 handleRemove(file, fileList) { //清空上传列表 this.$refs.uploadel.clearFiles(); this.loadupload = []; }, //验证上传文件格式大小 为true时可直接走后端上传接口 beforeImgUpload(file) { let isTrue = ""; let FileExt = file.name.replace(/.+\./, ""); //验证图片格式 if (["jpg", "jpeg", "bmp", "png", "gif", "mp4", "mkv", "avi"].indexOf(FileExt.toLowerCase()) === -1) { isTrue = false; } else { isTrue = true; } //验证图片大小 const isLt2M = file.size / 1024 / 1024 < 2; if (isTrue == false) { this.$message({ type: "warning", message: "请上传后缀名为jpg、jpeg、bmp、png、gif、mp4、mkv、avi的附件!", }); return false; } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!'); return false; } return true }, }, //文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 loadJsonFromFile(file, fileList) { if (file) { this.$nextTick(() => { //清除上传图片表单的prop校验规则 this.$refs.uploadelRef.clearValidate() }) } this.loadupload = fileList; }, //提交表单时候的操作带文件file submitForm(form){ this.$refs[form].validate((valid) => { if (valid) { //有上传图片和没上传图片的 if (this.loadupload.length > 0) { const formData = new FormData(); formData.append('file', this.loadupload[0].raw) //上传文件到后端 uploadfiles(formData).then(res => { this.loadupload = []; // 返回的图片名称 this.formData.handlePic = res.data.fileUrl //上传成功后走提交表单方法 }); } else { //提交表单方法 } } }) }, } </script>
includes验证图片格式
let types = ['image/png','image/jpeg']; const isJPG = types.includes(file.type); if (!isJPG) { this.$message({ type: 'error', message: this.$t('请上传 png或 jpg 格式的图片') }) return false; }
验证图片高度
//验证图片宽高 valWidthAndHeight(file) { const isSize = new Promise( (resolve, reject) => { let width = 108; let height = 108; let _URL = window.URL || window.webkitURL; let img = new Image(); img.onload = function () { let valid = img.width <= width && img.height <= height; valid ? resolve() : reject(); } img.src = _URL.createObjectURL(file); }).then(() => { return true; }, () => { return false; }); return isSize; }, //使用方法 this.valWidthAndHeight(this.loadupload[0].raw).then(res => { if(!res) { this.$message.closeAll(); this.$message.error('上传的图片最大尺寸为108*108!'); return } })