elementui文件上传
时间: 2023-10-04 17:13:25 浏览: 145
Element UI提供了上传文件的组件el-upload。使用el-upload需要先安装Element UI库。安装方法如下:
1. 使用npm安装Element UI库
```bash
npm i element-ui -S
```
2. 在需要使用上传组件的页面中引入Element UI库
```javascript
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
```
3. 在模板中使用el-upload组件
```html
<template>
<el-upload
class="upload-demo"
action="/upload"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:limit="3"
:file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</template>
```
上面的代码中,el-upload组件的属性说明如下:
- action:文件上传的地址
- on-success:文件上传成功后的回调函数
- before-upload:文件上传前的钩子函数
- limit:最多上传文件个数
- file-list:已上传的文件列表
在上传文件时,可以通过before-upload钩子函数对文件进行校验,例如限制文件大小、文件格式等。在上传成功后,可以通过on-success回调函数获取上传成功后的文件信息。
更详细的使用方法可以参考Element UI官方文档:https://element.eleme.io/#/zh-CN/component/upload
阅读全文
相关推荐


















