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
- 动力云→201 元 月 1GB 内存 10GB SSD 空间 10TB 虚拟空间(主机)
- ServerCheap→$2 月 1GB 内存 30GB SSD 空间 虚拟空间(主机)
- 腾讯云秒杀→1C2G1M 年付 88 元三年 268 元 2C4G3M 香港VPS[主机]
- 线路差-VIRPUS:$15.3 年 1.5T 流量 西雅图 VPS 3全球[VPS测评]
- 没有用过-Toxic-Host→€3.5 月 8GB 内存 80GB S全球[VPS测评]
- [11.11]ZJI→香港服务器 65 折 日本站群服务器 7 折 充 站群服务器[IP]
- Centos 停止 终止 Ping 命令全球[VPS测评]
- 企鹅小屋→2400 元 季 E5-2630L 16GB 内存 240GB香港VPS[主机]
- RAKsmart 双旦活动→独立服务器底价秒杀 I3 仅售$ 46 50独立服务器[U]
- NameCheap - 域名转入优惠 .com $4.88 年 .net全球[VPS测评]
- PQS→苏日 IPLC 莞港 IPL 等 KVM VPS 有一定的 DD全球[VPS测评]
- ZJI→香港云地&邦联服务器 65 折 香港葵湾特惠 6 折月付 450香港VPS[主机]
- 新商家慎重-LosAngelesVPS→$2.99 月 1 核 1G 不美国VPS[主机]
- Xhostserver→499 元 月 E3-1230v2 16GB 2全球[VPS测评]
- WordPress插件:Add-Document-Pages为你的网站增全球[VPS测评]
- Sharktech洛杉矶高防服务器,E5双核/1TB NVMe/不限流量全球[VPS测评]
- CloudIPLC → 双十一 泉州CN2 沪日IPLC NAT VPS全球[VPS测评]
- DMIT.io 香港VPS CVM N3系列香港VPS[主机]
- HKServerSolution 七夕优惠汇总 香港 HKBN 8 折 香港VPS[主机]
- 免备案服务器对权重和网站排名有影响吗?全球[VPS测评]
- EdgeNat双十二优惠:韩国/香港/美国全场VPS年付,2核2G2M仅韩国VPS[主机]
- 源云主机,香港沙田VPS CN2 1核1G 24/月 NTT 50M 9香港VPS[主机]
- 极光kvm怎么样,便宜大带宽香港cmi vps/美国gia vps低至1美国VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
- cloudpowerall:美国洛杉矶CN2 VPS,年付$9.99;香美国VPS[主机]
- 老六云怎么样?香港cn2vps月付23元香港NTT月付9.9元香港VPS[主机]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美全球[VPS测评]
- 恒创科技怎么样?香港美国云服务器/独服/高防全场2.5折起美国VPS[主机]
- 跨境电商平台首选香港服务器香港VPS[主机]
- 小鹿云计算:香港双程cn2首月6折优惠;美国300G高防-8元/月;香港站群服务器[IP]
转载请注明原文地址:http://140.238.13.167:12355/read-210507.html











