elementui上传图片回显
时间: 2023-08-31 13:05:14 浏览: 99
Vue+elementUI实现多图片上传与回显功能(含回显后继续上传或删除)
ElementUI提供了一个Upload组件用于上传文件,上传成功后可以通过设置response属性来获取上传成功后的信息,并将其展示在页面上。
具体步骤如下:
1. 在HTML中添加Upload组件,并设置action属性为上传接口的URL。
```
<template>
<el-upload
class="upload-demo"
action="/upload"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:auto-upload="false"
:list-type="'picture-card'"
:file-list="fileList">
<i class="el-icon-plus"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</template>
```
2. 在data中添加fileList属性,用于保存上传成功后的文件信息。
```
data() {
return {
fileList: []
}
}
```
3. 在methods中添加handleSuccess方法,用于处理上传成功后返回的数据。
```
methods: {
handleSuccess(response, file, fileList) {
this.fileList.push({
name: file.name,
url: response.url
})
}
}
```
4. 在上传成功后,将上传成功的图片展示在页面上。
```
<el-col :span="6" v-for="item in fileList" :key="item.name">
<div class="image">
<img :src="item.url" alt="">
</div>
</el-col>
```
完整代码如下:
```
<template>
<div class="upload-demo">
<el-upload
class="upload-demo"
action="/upload"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:auto-upload="false"
:list-type="'picture-card'"
:file-list="fileList">
<i class="el-icon-plus"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<el-row>
<el-col :span="6" v-for="item in fileList" :key="item.name">
<div class="image">
<img :src="item.url" alt="">
</div>
</el-col>
</el-row>
</div>
</template>
<script>
export default {
data() {
return {
fileList: []
}
},
methods: {
handleSuccess(response, file, fileList) {
this.fileList.push({
name: file.name,
url: response.url
})
}
}
}
</script>
<style scoped>
.upload-demo .el-upload__tip {
margin-top: 10px;
}
.upload-demo .image {
width: 100%;
height: 200px;
overflow: hidden;
margin-bottom: 20px;
}
.upload-demo .image img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
```
阅读全文