Element UI 实现上传图片的功能
只需要把
<el-upload action="">中的action等于增加方法的地址,即ajax中url等于的东西
上传图片时,默认name=file
Element UI 实现上传图片功能的代码如下:
<template>
<!--<div>轮播</div>-->
<div class="login-box">
<el-upload
class="upload-demo"
action="http://localhost:8080/lunbo/insert"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
multiple
:limit="3"
:on-exceed="handleExceed"
:file-list="fileList">
<el-button size="small" type="primary" >点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</div>
</template>
<script>
export default {
name: "add-file",
data(){
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
methods:{
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${ file.name }?`);
},
addfile(formName){
this.$refs[formName].validate((valid) => {
var vm=this;
if(valid){
console.log(vm.form.url);
this.$axios({
method:'post',
url:'http://localhost:8080/lunbo/insert',
enctype:'multipart/form-data',
data:{
url:this.form.url
}
}).then(function (resp) {
console.log(resp.data);
if(resp.data=="success"){
console.log('真棒');
/*vm.$router.push("/UserList")*/
vm.$message({
message: '恭喜你,添加成功',
type: 'success'
});
}else{
vm.$message.error('添加失败');
return false;
}
})
}
});
}
}
}
</script>
<style scoped>
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)