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
- 阿里云双 11 企业新用户特惠→2 核 8G6M 1645 元 3 年 全球[VPS测评]
- 特价 $69 月 E3-1230v2 16G 内存 2T 硬盘 10T美国VPS[主机]
- $49 年 512M 内存 80G 硬盘 500G 流量 1Gbps K全球[VPS测评]
- ZJI - 春节促销全场八折 香港云地四型独服买 2 月送 1 月香港VPS[主机]
- 疯狂猜成语 图猜成语一颗心流出很多血是什么成语?全球[VPS测评]
- Vultr 优惠活动,2020 年新用户送 103 美元(支持支付宝),全球[VPS测评]
- DediPath 元旦优惠→VPS 全场 8 折 Hybrid Serv全球[VPS测评]
- 帝国 CMS 上传文件大小及类型限制的解决方法全球[VPS测评]
- 微信开发→"errcode": -1000,"errmsg": "sys全球[VPS测评]
- 299 元 年 1G 内存 15G SSD 2Mbps 不限量 XEN 香港VPS[主机]
- Chrome 浏览器提示→Adobe Flash Player 更新完毕全球[VPS测评]
- TMThosting→$3.11 月 KVM-512MB 14G SSD全球[VPS测评]
- 三优云→香港 CMI 日本 CN2 美国 CN2 直连 VPS 促销 终日本VPS[主机]
- BGPTO→637 元 月 E3-1230v3 8GB 2TB 5IP 全球[VPS测评]
- 10g.biz 洛杉矶三网直连/CN2GIA独服 七折 $97.3起/月全球[VPS测评]
- 数据中心助力光纤光缆行业发展全球[VPS测评]
- 什么是云服务器?云服务器怎么登陆?全球[VPS测评]
- 云计算的大门依旧没向雷军敞开全球[VPS测评]
- 轻云互联,香港VPS月付22元 美国VPS月付19元美国VPS[主机]
- 亚当云怎么样?香港安畅cn2vps带宽10M月付28元香港VPS[主机]
- 20个温暖心灵的问题,抖音,朋友圈都可以拿去发。全球[VPS测评]
- anyhk香港HKT商宽NAT VPS,1Gbps无限流量,终身8折¥3香港VPS[主机]
- 桔子数据,香港安畅BGP大带宽VPS 首月五折体验 三网直连 14元/月香港VPS[主机]
- 奇维云优惠活动:香港CN21核1G1M带宽云服务器免费领取香港VPS[主机]
- 永恒云:江苏挂机宝6元/月,60元/年;香港建站vps仅18元;深圳BG香港VPS[主机]
- 创e云:香港CN2VPS优惠专区,4核/4G/20G/2Mbps不限流量香港VPS[主机]
- 快速云:云服务器和vps区别是什么云服务器和vps哪个比较安全2022-全球[VPS测评]
- 企鹅小屋:1核/512MB/10GB SSD空间/500GB流量/KVM虚拟空间(主机)
- Vmshell:香港免费vps试用机24H,新增在线测速!年付再打9折,香港VPS[主机]
- JGKVM活动钜惠:美国cn2,香港cmi,大带宽vps,三网直连,10美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-204622.html











