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
- 2020 年美国虚拟主机 空间推荐 排名不分先后虚拟空间(主机)
- QSVM→新加坡 KVM VPS 可选限制流量或不限流量;256MB 内全球[VPS测评]
- js 错误→xx is not defined at HTMLAncho全球[VPS测评]
- PuzCloud→31.5 元 月 1GB 内存 20GB 空间 1TB虚拟空间(主机)
- 优惠 海星云→圣何塞直连高防 VPS 日本直连 VPS 全场 8 折优惠日本VPS[主机]
- 存储 VPS $5 月 1G 内存 1T 硬盘 2T 流量 1Gbps 全球[VPS测评]
- 阿里云 2020 上云采购季新用户专场,云服务器低至 0.6 折,买 E全球[VPS测评]
- 黑五 CloudCone→三款特价机器 512M 内存年付低至 15 美全球[VPS测评]
- ReliableSite→纽约独服 i5-4570 16GB 内存 2T全球[VPS测评]
- 抢购 HostDare→亚洲优化线路 65 折优惠 1 核 756M 内全球[VPS测评]
- DEDECMS 提示 Error:no csrf hash code!错全球[VPS测评]
- 看奈飞 VPS ¥29 月 512M 15G 硬盘 1T 流量 100M全球[VPS测评]
- pacificrack→5 月特价便宜 VPS $9.89 年 KVM 全球[VPS测评]
- Vultr VPS主机取消 $3.5入门机型及新用户赠送$25全球[VPS测评]
- dedicatserver.ro罗马尼亚抗诉VPS 2核4G内存/30G全球[VPS测评]
- 使用测速脚本对国外VPS服务器进行测速全球[VPS测评]
- Bangmod-4.6$ 月 泰国曼谷 1核512M内存20G硬盘 1T全球[VPS测评]
- 桔子数据,香港安畅BGP大带宽VPS 首月五折体验 三网直连 14元/月香港VPS[主机]
- 消费复苏形势良好,我们将多措并举实施扩大内需政策全球[VPS测评]
- 香港云服务器怎么选?云服务器性能测评对比香港VPS[主机]
- 桔子数据:1核1GB香港CN2 GIA vps促销28元/月,最高30M香港VPS[主机]
- 华凯易佰同比扭亏跨境电商行业回暖H2旺季将至中报追踪全球[VPS测评]
- 免备案服务器对权重和网站排名有影响吗?全球[VPS测评]
- 极客主机,优惠8折充值返25%,香港CN2VPS,日本软银VPS,新加坡日本VPS[主机]
- 好云:国内云服务器2核2G5M优惠,仅350元/年;香港/海外云服务器,香港VPS[主机]
- 做跨境电商一个月都有多少收入?全球[VPS测评]
- 租用美国服务器有哪些安全方案美国VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
- Mysql连接数过高,如何检查?全球[VPS测评]
- 热网互联:香港/日本云服务器2核4G低至104元/月_青岛/日本独立服务独立服务器[U]
转载请注明原文地址:http://140.238.13.167:12355/read-219484.html











