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
- NameCheap - 域名转入优惠 .com $4.88 年 .net全球[VPS测评]
- 补货通知 搬瓦工→CN2 GIA 线路$46.87 年机器补货了 速度上全球[VPS测评]
- Sharktech→KVM 七五折 2GB 30G SSD 4TB 洛杉全球[VPS测评]
- 羊毛太小-VPSLOT→0.4 美元首月 1 核 512M 500G 流美国VPS[主机]
- SaltyfishTech→$40 年 1GB 内存 30GB SSD 虚拟空间(主机)
- 黑五 Virtono→全场 VPS 季付 半年付 7 折优惠 部分套餐年全球[VPS测评]
- 腾讯云服务器 2020 年 2 月低价促销活动,2 核 4G3M998 全球[VPS测评]
- Cubecloud - 魔方云春节 75 折优惠 香港新加坡等机房可选香港VPS[主机]
- 阿里云 618 优惠活动→云服务器低至 2 折仅需 190 元 年起 新全球[VPS测评]
- WordPress 插件:WP Last Modified Info 告全球[VPS测评]
- 红米 Redmi K20Pro MIUI 11 欧版 ROM 刷机教程全球[VPS测评]
- Solarcom → 瑞士 7.8$ 月 2C2G40G硬盘 1T流量 独立服务器[U]
- Dominios → 6.2€月付 1C2G20G硬盘 葡萄牙 无限流量全球[VPS测评]
- Yatosha → 坦桑尼亚VPS 90$ 月 1C2G20G硬盘 OV全球[VPS测评]
- Cloudmild-10$ 月 印尼 1C1G25G硬盘 1Gbps不限全球[VPS测评]
- Spry servers-凤凰城 月付$2.5 1核512M内存5G硬盘全球[VPS测评]
- HKServerSolution → 4G内存 20G SSD 3T流量全球[VPS测评]
- 很简单的图片不规则布局样式,纯css样式实现的图片瀑布流布局全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- 跨境电商有哪些平台?十大跨境电商平台!全球[VPS测评]
- 快速云:云服务器vps的区别是什么云服务器和vps哪个比较好2022-0全球[VPS测评]
- VPS成立BurjeelHoldings全球[VPS测评]
- 互盟云怎么样?1核2G云服务器仅468元/年,香港云主机58元/月香港VPS[主机]
- 恒创主机优惠码85折_服务器优惠券8折_恒创科技优惠_代金券全球[VPS测评]
- 做跨境电商,如何打造私域流量?全球[VPS测评]
- 傲闪云vps,1核1G低至10元起,国内高防/香港CN2/美国CN2/美美国VPS[主机]
- 六六云:美国cera机房CN2 GIA高防/香港CN2 GIA建站/CM美国VPS[主机]
- 恒星云:喜迎新春香港CN2云服务器,首月9.9元;深圳2核4G云服务器仅香港VPS[主机]
- 香港cn2线路服务器哪里好?香港cn2服务器推荐哪家好?香港VPS[主机]
- 易探云:香港bgp服务器1核1G5M带宽/10G数据盘,首月19元香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-219484.html











