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
- ftpit→精致型商家 $1.49 月 KVM 256M 内存 1 核 全球[VPS测评]
- 线路不行-海星云香港大浦 NTT 机房 KVM VPS 服务器上新 电信香港VPS[主机]
- 促销 极光 KVM→4 月新增日本东京 CN2 产品 美国洛杉矶双向 C日本VPS[主机]
- 稳定 RAKsmart→圣何塞 100M 不限流量服务器 61.38 美全球[VPS测评]
- centerhop→优质新加坡 VPS $27 年 1g 内存 25gSWINDOWS
- [11.11]HostXen→充 300 送 100 续费减 10 元 全球[VPS测评]
- ThinkPHP 高危安全漏洞 攻击者可直接获取服务器权限 请尽快升级官全球[VPS测评]
- 疯狂猜成语 图猜成语一个健壮的人身体里有一个瘦弱的人是什么成语?全球[VPS测评]
- TP3.*版本 Class 'ThinkLog' not found 错全球[VPS测评]
- Ftpit→$15 年 OpenVZ-512MB 10G SSD 1TB全球[VPS测评]
- centOS7系统下如何修改服务器SSH端口默认的22。全球[VPS测评]
- robots.txt与nofollow之间的区别全球[VPS测评]
- 又入搬瓦工VPS年付49.99刀 CN2 GIA 限量版方案全球[VPS测评]
- 如何检测WordPress博客的访问速度?全球[VPS测评]
- HostHatch 大容量VPS 1G 1T HDD 2T $5 月全球[VPS测评]
- Kimsufi KS3(法国) & KS7(CA) 促销全球[VPS测评]
- PumpCloud 台湾VPS SeedNet和HiNET → 流媒体解全球[VPS测评]
- 什么是VPS?国内VPS有哪些?全球[VPS测评]
- 搬瓦工VPS:高端线路助力企业运营,10Gbps美国 cn2 gia,1日本VPS[主机]
- tmhhost官网登录不上怎么办?日本软银/美国CN2 GIA/美国高防日本VPS[主机]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
- 硅云香港云服务器促销活动:CPU性能100%性能,无需备案香港VPS[主机]
- 又有美议员窜访台,中国驻美大使馆:“中方将对美国的挑衅采取果断的反制措施美国VPS[主机]
- HostKvm:韩国香港vps10月份最新促销,海外VPS全场八折优惠韩国VPS[主机]
- sugarhosts糖果主机黑五:VPS服务器5折,最低月付24.5元,香港VPS[主机]
- 青云互联:香港vps全场终身8折优惠,CN2线路,提供大带宽vps,月付香港VPS[主机]
- 速云互联:香港GT大带宽vps/香港CN2云服务器/洛杉矶200G高防v香港VPS[主机]
- 2022年亚马逊Prime会员日将于7月12日和13日举行,覆盖24个站全球[VPS测评]
- locvps:香港云地多线路BGP云服务器,三网直连,6折终身优惠中,稳香港VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-204622.html











