Element ui是由饿了么团队推出的基于vue的前端库,功能非常强大,其中的upload组件可以轻松的实现前端通过点击或者拖拽上传文件。不过upload组件默认的批量上传却是逐项上传,也就是你一次批量选择5个文件,那么会发送5次请求来分别上传这5个文件。
那么Element ui中upload组件怎么实现一次请求批量上传呢?这就需要通过自定义http-request来覆盖默认的上传行为,实现自定义上传,具体代码如下:
<template> <d2-container> <el-upload class="upload-demo" ref="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.$refs.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.$refs.upload.submit(); fileUpload(this.fileData).then(response => { //console.log(response) }) },
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Element ui中upload组件一次请求批量上传》
文章链接:https://www.liuzhanwu.com/3446.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
文章名称:《Element ui中upload组件一次请求批量上传》
文章链接:https://www.liuzhanwu.com/3446.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
相关推荐
Rank Math 2026黑五优惠:PRO首年$59.88,Rank Math AI $3.99/月起,最高省40%
Rank Math 2026万圣节优惠:PRO版€5.59/月起,最高41%折扣、关键词跟踪额度翻倍
WP Rocket 2026万圣节优惠:年付8折、3年付6折,单站授权$2.97/月起
CubePath GraphQL API 使用教程:统一查询VPS CPU、内存、磁盘及带宽监控数据
Onchaty 云端在线客服:一行 JS 接入网站,支持 AI 自动回复,无需额外部署 VPS
宝塔面板2026年1024程序员节特惠:Linux永久授权2888元起,SSL证书低至5折,企业版赠证书
KVM、OpenVZ、LXC 有什么区别?2026年购买 VPS 如何选择虚拟化技术
Porkbun 10月域名促销:.xyz首年86%优惠,.buzz减免92%,.shop首年$1起
