一、上传单个图片
<el-form-item label="图片" prop="image">
<el-upload
class="dl-avatar-uploader-min square"
:action="uploadUrl"
:show-file-list="false"
:on-success="handleUpImage"
:before-upload="beforeImageUpload"
list-type="picture"
accept="image/*"
>
<el-image v-if="addform.image" :src="addform.image" class="avatar"></el-image>
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
<template #tip>
<div class="el-upload__tip">只能上传图片,且单张图片大小不能超过10MB</div>
</template>
</el-upload>
</el-form-item>
export default {
setup() {
const dataSet = reactive({
uploadUrl: baseurl+'/api/common/upload',
addfrom:{
image:''
}
})
const methods = {
// 上传前,限制的上传图片大小
beforeImageUpload(rawFile){
if(rawFile.size / 1024 / 1024 > 10){
ElMessage.error("单张图片大小不能超过10MB!");
return false;
}
return true;
},
// 上传成功,获取返回的图片地址
handleUpImage(res){
dataSet.addform.image = res.data.url;
},
}
}
}
二、上传多张图片,超过限制隐藏上传按钮
<el-form-item label="封面图片" :prop="addform.ppid!=6?'covers':''">
<el-upload
class="dl-avatar-uploader-min square"
:class="{uoloadBtn:showBtnDealImg,disUoloadBtn:noneBtnImg}"
:action="uploadUrl"
:limit="limitCountImg"
:on-success="coverFileSuccess"
:on-remove="coverFileRemove"
:on-exceed="handleExceedCover"
:before-upload="beforeImageUpload"
:on-change="handleImgChange"
:file-list="fileList"
list-type="picture-card"
accept="image/*"
multiple
>
<el-button type="text">上传图片</el-button>
<template #tip>
<div class="el-upload__tip">最多上传9张图片,且单张图片大小不能超过10MB</div>
</template>
</el-upload>
</el-form-item>
.disUoloadBtn .el-upload--picture-card{
display:none; /* 上传按钮隐藏 */
}
export default {
setup() {
const dataSet = reactive({
uploadUrl: baseurl+'/api/common/upload',
coversList:[],
limitCountImg:9,
showBtnDealImg:true,
noneBtnImg:false,
})
const methods = {
handleImgChange(file, fileList){
dataSet.noneBtnImg = fileList.length >= dataSet.limitCountImg;
},
beforeImageUpload(rawFile){
if(rawFile.size / 1024 / 1024 > 10){
ElMessage.error("单张图片大小不能超过10MB!");
return false;
}
return true;
},
coverFileSuccess(response, file, fileList) {
if(response.code==0){
let obj = {};
obj.name = file.name;
obj.url = response.data.url;
dataSet.coversList.push(obj);
}else{
ElMessage.error({
message: response.msg,
type: "error",
});
}
},
coverFileRemove(file, fileList) {
dataSet.coversList.forEach((item, index) => {
if (file.name == item.name) {
dataSet.coversList.splice(index, 1);
}
});
dataSet.noneBtnImg = fileList.length >= dataSet.limitCountImg;
},
handleExceedCover(files, fileList){
ElMessage.error({
message: `上传图片数量超出限制!`,
type: "error",
});
},
}
}
}
原文地址:https://blog.csdn.net/thingir/article/details/124802295
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_42106.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。