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
- Mineserver:香港CMI大带宽云服务器38元/月起(300M不限 香港VPS[主机]
- 快快数据测评:香港CN2云服务器,2核/1G/5M带宽,低至24元/月 香港VPS[主机]
- 什么是IconFont ?iconfont 的使用和解析 全球[VPS测评]
- WHMCS 8.1如何简化注册页面 全球[VPS测评]
- 云服务器能干什么?有几十种玩法?阿里云,腾讯云服务器如何购买? 全球[VPS测评]
- 关键路径、资金情况、政策关键数据中心的可持续发展路径在哪里? 全球[VPS测评]
- Bloggar-移动端友好的新闻博客类HTML5模板 全球[VPS测评]
- 世界杯季疫情突发,跨境电商订单排队,义乌商人面临难题 全球[VPS测评]
- 云计算的大门依旧没向雷军敞开 全球[VPS测评]
- 一篇漫画,看懂云计算 全球[VPS测评]
- 恒创科技怎么样?香港美国云服务器/独服/高防全场2.5折起 美国VPS[主机]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企 美国VPS[主机]
- 香港vps免费:青云互联,1核1G/50G硬盘/500GB流量/2M带宽 香港VPS[主机]
- 菠萝云服务器怎么样?香港云主机BGP、CN2,2核1G2M带宽仅49元/ 香港VPS[主机]
- 蓝队云服务器怎么样?蓝队云服务器价格和优势是什么? 全球[VPS测评]
- 阿里云香港服务器多少钱?阿里云香港服务器价格 香港VPS[主机]
- RAKsmart促销活动:韩国服务器低价抢购,美国G口服务器低至$99, 韩国VPS[主机]
- 遨游主机:8折优惠,美国cn2 gia vps,54元/月,2G内存/1 美国VPS[主机]
- 茶猫云:香港云服务器,支持windows,2M带宽,34元/月 WINDOWS
- 无忧云:雅安/德阳100G高防云主机月付78元起,香港CN2云服务器月付 香港VPS[主机]
- 樊云:9折优惠,香港cn2 vps+(强制三网、50G高防);美国cn2 美国VPS[主机]
- 尊云:河南BGP云服务器2核4G仅68.6元/月,823.2元/年;香港 香港VPS[主机]
- 华纳云新春活动:香港CN2云服务器4核4G5M仅3476元/3年;美国云 美国VPS[主机]
- 41云:香港bgp云服务器低至14元/月;香港三网CN2 GIA VPS 香港VPS[主机]
- 香港vps免备案服务器可以省掉哪些烦恼?选择哪几家好? 香港VPS[主机]
- 常见的CN2,GIA,CIA 各个网络线路解释 全球[VPS测评]
- 6种样式社交论坛网站HTML5模板 - Social 全球[VPS测评]
- 什么是Bootstrap? 全球[VPS测评]
- 消费复苏形势良好,我们将多措并举实施扩大内需政策 全球[VPS测评]
- 文物、沉船、村落!干旱导致大量遗迹重见天日 2022年干旱还要持续多久? 全球[VPS测评]
转载请注明原文地址: http://140.238.13.167:12355/read-213480.html











