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
- 黑五 Hostdare→4 折优惠 CN2 GIA 超大硬盘 做站首选全球[VPS测评]
- Uncaught SyntaxError: Block-scoped d全球[VPS测评]
- racknerd 大带宽大流量美国 vps 低至 8 元 月起,1 核 美国VPS[主机]
- hosts 文件位置全球[VPS测评]
- 无污染的智能 DNS 搭建-overture全球[VPS测评]
- Rainbow Cloud→$50 月 E3-12XX 16G 内存 1香港VPS[主机]
- Aurora→日本 CN2 GIA 线路 KDDI 线路 可以解锁 ne日本VPS[主机]
- Dedicated→$35 月 E3-1270 8GB 内存 1TB 硬全球[VPS测评]
- Justhost→俄罗斯 KVM 八折 87 卢布起 200M 无限流量全球[VPS测评]
- CI 框架提示 Undefined property: XXX::$db全球[VPS测评]
- WordPress插件:Simple URLs 链接点击统计管理全球[VPS测评]
- WordPress无插件自动实现标签关键字内链全球[VPS测评]
- WordPress博客网站搜索引擎优化需要注意几个SEO细节全球[VPS测评]
- 如何让WordPress博客变得更加安全全球[VPS测评]
- TmhHost双十一促销,日本/香港/美国CN2 GIA高防季付79.2日本VPS[主机]
- 搬瓦工VPS方案怎么选?全球[VPS测评]
- Unesty 1欧德国鸡 测试记录(已开走)全球[VPS测评]
- 50KVM VPS免费自助换IP功能全球[VPS测评]
- VPC.KR 韩国 IPLC NAT 评测韩国VPS[主机]
- 拼多多也要做跨境电商?出海之路能一帆风顺吗,了解一下全球[VPS测评]
- 向日葵-漏洞科普:海外云服务器三种漏洞修复方法快收藏起来!全球[VPS测评]
- 世界杯季疫情突发,跨境电商订单排队,义乌商人面临难题全球[VPS测评]
- 2020年国外VPS哪个最好?教你学会选择国外VPS全球[VPS测评]
- 企鹅小屋:1核/512MB/10GB SSD空间/500GB流量/KVM虚拟空间(主机)
- 2022年有哪些顶级托管服务提供商全球[VPS测评]
- 快速云:科普云服务器服务器的概念和作用2022-08-2313:26来源全球[VPS测评]
- 云计算竞争日趋白热化云计算行业市场前景分析全球[VPS测评]
- 冀青云怎么样?香港CN2-GIA vps月28元/香港NTT 月9元香港VPS[主机]
- 轻云互联,香港美国vps终身8.8折17元/月起,cn2直连线路,50G美国VPS[主机]
- 遨游主机:8折优惠,美国cn2 gia vps,54元/月,2G内存/1美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-216472.html











