el-upload手动上传图片,限制图片大小、格式
2021-02-07 22:17
标签:and fst sele eject exce enum 部分 image medium 部分代码: template部分 methods: 表单提交methods: el-upload手动上传图片,限制图片大小、格式 标签:and fst sele eject exce enum 部分 image medium 原文地址:https://www.cnblogs.com/duanzhenzhen/p/13086510.htmlhandleExceed(files) {
this.$message.warning(‘当前限制选择1个文件‘);
},
//删除文件
handleRemove(file,fileList) {
console.log(file);
fileList.splice(0,1);
this.fileList=fileList;
this.uploadFile = null;
console.log(‘3333333‘,this.fileList);
this.changeUrl = file.url;
},
httpRequest(params){
const file = params.file;
const isJPG = (file.type === ‘image/png‘) || (file.type === ‘image/jpg‘) || (file.type === ‘image/jpeg‘);
const isLt5M = file.size / 1024 / 1024 ;
this.uploadFile = file || null;
//解决删除文件时报错
// const prom = new Promise((resolve, reject) => {})
// prom.abort = () => {}
// return prom
const prom = new Promise((resolve, reject) => {
if (!isJPG) {
this.$message({
type:‘error‘,
message:‘上传图片只支持JPG、JPEG、PNG格式!‘
})
}else if (!isLt5M) {
this.$message({
type:‘error‘,
message:‘上传图片大小不能超过 5MB!‘
})
}
return isJPG && isLt5M;
resolve();
})
prom.abort = () => {}
return prom
},cancleUpload(){
this.alertBox = false;
this.fileList = [];
},// 上传提交
uploadSucess(formName){
this.$refs.uploadimg.submit();
let formData = new FormData();
console.log(‘22222222‘,this.uploadFile,!this.uploadFile);
if(this.addText=="add"){
console.log(‘..........‘,this.uploadFile);
if(!this.uploadFile){
this.$message.error(‘图片不能为空!‘)
}else{
formData.append("file",this.uploadFile);
formData.append("title",this.addForm.knowledgeTitle);
formData.append("content",this.addForm.knowledgeContent);
formData.append("status",this.addForm.knowledgeStatus);
this.$refs[formName].validate((valid)=>{
if(valid){
this.$store.dispatch("baseSet/uploadSucess",formData)
.then(()=>{
this.knowledgeManager(this.pageNum);
this.alertBox = false;
this.$refs.uploadimg.clearFiles();
this.ifStatus = false;
})
}
})
}
}else{
let changeStatus = null;
if(this.ifStatus){
changeStatus = this.addForm.knowledgeStatus;
}else{
this.addForm.knowledgeStatus==‘可用‘?changeStatus=0:changeStatus=1;
}
console.log(this.addForm.knowledgeTitle);
if(this.uploadFile){
formData.append("file",this.uploadFile);
};
if(this.addForm.knowledgeTitle==‘‘){
this.$message.warning("请添加标题");
}else if (this.addForm.knowledgeContent==‘‘) {
this.$message.warning("请添加内容");
}else{
formData.append("title",this.addForm.knowledgeTitle);
formData.append("content",this.addForm.knowledgeContent);
formData.append("status",changeStatus);
formData.append("id",this.changeId);
this.$store.dispatch("baseSet/changeKnowledge",formData).then(()=>{
this.knowledgeManager(this.pageNum);
this.alertBox = false;
this.ifStatus = false;
})
}
}
},
文章标题:el-upload手动上传图片,限制图片大小、格式
文章链接:http://soscw.com/index.php/essay/52358.html