Element ui 中 upload 组件一次请求批量上传
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

Element ui 是由饿了么团队推出的基于 vue 的前端库,功能非常强大,其中的 upload 组件可以轻松的实现前端通过点击或者拖拽上传文件。不过 upload 组件默认的批量上传却是逐项上传,也就是你一次批量选择 5 个文件,那么会发送 5 次请求来分别上传这 5 个文件。
那么 Element ui 中 upload 组件怎么实现一次请求批量上传呢?这就需要通过自定义 http-request 来覆盖默认的上传行为,实现自定义上传,具体代码如下:
<template> <d2-container> <el-upload class="upload-demo" ="upload" action="" :http-request="customUpload" :before-upload="beforeFileUpload" :on-preview="handlePreview" :on-remove="handleRemove" :on-success="handleSuccess" :file-list="fileList" :auto-upload="false" accept=".dbf" multiple> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> <!--div slot="tip" class="el-upload__tip">只能上传 jpg/png 文件,且不超过 500kb</div>--> </el-upload> </d2-container> </template> <script> export default { data() { return { fileList: [], fileData: new FormData() }; }, methods: { submitUpload() { this.$s.upload.submit(); fileUpload(this.fileData).then(response => { }) }, beforeFileUpload(file){ let extension=file.name.substring(file.name.lastIndexOf('.')+1); const isDbf = extension === "dbf"; const isLt10M = file.size / 1024 / 1024 < 10 if (!isDbf) { this.$message.error('上传的数据文件只能是 dbf 格式!'); } if (!isLt10M) { this.$message.error('上传的单个数据文件大小不能超过 10MB!'); } return isDbf && isLt10M; }, handleRemove(file, fileList) { }, handlePreview(file) { }, handleSuccess(response,file, fileList) { this.$message.success('上传成功') }, customUpload(file) { this.fileData.append('files[]', file.file) return false } } } //引入 axios import request from '@/plugin/axios' export function fileUpload(file) { return request({ method: 'post', url: 'http://localhost/api/import', headers: {'Content-Type':'multipart/form-data'}, data: file }) } </script>代码说明:
1.配置自定义的 http-request 函数:
:http-request="customUpload"2.在 data 中添加一项参数:fileData,类型是 FormData:
fileData: new FormData()3.实现 customUpload 方法,将上传文件信息拼接到 fileData 中。
customUpload(file) { this.fileData.append('files[]', file.file) return false }4.定义 axios 异步上传方法:
export function fileUpload(file) { return request({ method: 'post', url: 'http://localhost/api/import', headers: {'Content-Type':'multipart/form-data'}, data: file }) }5.在 submitUpload 中调用 4 中定义的上传方法:
submitUpload() { this.$s.upload.submit(); fileUpload(this.fileData).then(response => { //console.log(response) }) },猜你可能想看的VPS
- Pyclouds→45 元 月 256MB 内存 5GB 空间 100G虚拟空间(主机)
- 新年促销 RackNerd→1 核 1.25G 18G SSD 3000全球[VPS测评]
- 上新极光 KVM→美国洛杉矶 PZ 中美优化线路 KVM 123.45 美国VPS[主机]
- 按时计费 $2.8 月 512M 内存 10G 硬盘 1T 流量@1Gb全球[VPS测评]
- 标准互联→全场云服务器 8 折 香港云服务器年付 256 元起 美国年付美国VPS[主机]
- 新商家慎重-iara.cloud→日本 CN2 GIA 日本软银 KVM日本VPS[主机]
- 补货通知 搬瓦工→CN2 GIA 线路 512M 内存套餐补货了 年付 全球[VPS测评]
- Bootstrap-datepicker 设置开始时间及结束时间可选范围全球[VPS测评]
- VPC.kr→$14.99 月 100G 流量 200Mpbs 宽带 全球[VPS测评]
- Sharktech→洛杉矶高防 VPS 七五折 KVM 2G 套餐月付 全球[VPS测评]
- Domeny.pl→.pl .com.pl .edu.pl .site 全球[VPS测评]
- 不要再相信这4个错误的Google 搜索引擎SEO优化观念全球[VPS测评]
- 更新WordPress网站PHP7.2新版本的注意事项全球[VPS测评]
- 什么是阿里云 ECS 服务器全球[VPS测评]
- 如何检测WordPress博客的访问速度?全球[VPS测评]
- Linedata 俄罗斯不限流量VPS测评 低至月付1欧全球[VPS测评]
- Serverum-俄罗斯圣彼得堡 拉脱维亚 5$ 月 1核1G10GB内全球[VPS测评]
- HKServerSolution → 1核心 2G内存 20G SSD 全球[VPS测评]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
- 74块钱一年的云服务器可以用来做什么?全球[VPS测评]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- 桔子数据,香港安畅BGP大带宽VPS 首月五折体验 三网直连 14元/月香港VPS[主机]
- 2020年国外VPS哪个最好?教你学会选择国外VPS全球[VPS测评]
- 品光网络:香港美国免备案云服务器超低价,2核2G2M年付仅750元/年美国VPS[主机]
- PQhosting:香港HE/乌克兰/俄罗斯/荷兰/摩尔多瓦vps主机/香港VPS[主机]
- CombCloud,香港沙田cn2vps终身七折起,高质量网络稳定低延迟香港VPS[主机]
- 买了一台云服务器到底能干什么?全球[VPS测评]
- 魔方云:香港原生IP vps/大带宽,月付79元起;美国洛杉矶CN2 G美国VPS[主机]
- UCloud海外云服务器促销:全场1折起,香港cn2vps最低年付150香港VPS[主机]
- 创意IT服务公司主页网站模板 - Softets全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-210507.html











