当前位置: 首页 > news >正文

Vue Element upload组件和Iview upload 组件上传文件

今天要分享的是使用这俩个UI组件库的upload组件分别实现调用组件本身的上传方法实现和后台交互。接下来就是开车的时间,请坐稳扶好~

一、element upload组件传送门

 1、html文件

<el-upload ref="uploadRef" :action="uploadUrl" :data="dataObj" :multiple="true" :before-upload="beforeUpload":on-success="handleSuccess" :auto-upload="false"><template #trigger><el-button type="primary">文件选择<Icon type="md-arrow-round-up" /></el-button></template><el-button @click="submitUpload">确认上传<Icon type="md-arrow-round-up" /></el-button>
</el-upload>

注意事项: 使用组件本身的上传事件,必须加auto-upload属性设置为false;

                    beforeUpload方法除校验外,外层不允许返回return false;

2、js文件

export default {methods: {beforeUpload(file) {console.log("文件", file)// 上传文件接口额外参数this.dataObj.businessCode = "ISSUEPOINT";this.dataObj.salesType = "SALES12"const { name, size } = file;const index = name.lastIndexOf('.');// 判断文件名是否有后缀,没后缀文件错误if(index === -1) {this.$notify.error({title: '错误',message: '文件错误,请重新上传!',});return false;}const fileType = name.substr(index + 1);const acceptFileTypes = ['txt', 'zip', 'rar'];// 判断文件类型if(!acceptFileTypes.includes(fileType)) {this.$notify.error({title: '错误',message: '文件类型错误,请重新上传!',});return false;}// 判断文件大小if(size > 10*1024*1024) {this.$notify.error({title: '错误',message: '文件大小超过10M,请重新上传!',});return false;}this.fileLists.push(file)},submitUpload() {//使用ref调用组件本身的submit方法上传文件this.$refs.uploadRef.submit()}}
}

二、iview upload 组件传送门

 

1、html文件

<Upload ref="upload" :multiple="true" :action="uploadUrl" :data="fileUploadObj" :before-            upload="beforeUpload" :on-success="handleSuccess" :auto-upload="false" :show-upload- list="false"
><Button>选择文件<Icon type="md-arrow-round-up" /></Button>
</Upload>

注意:使用iview upload组件调取自身上传方法,beforeUpload方法必须要返回false,和element upload相反

2、js文件

export default {methods: {beforeUpload(file) {console.log("文件", file)// 上传文件接口额外参数this.dataObj.businessCode = "ISSUEPOINT";this.dataObj.salesType = "SALES12"// 上传文件其他的校验方法let imgTypeArr = ["image/png", "image/jpg", "image/jpeg","image/gif"]let imgType = imgTypeArr.indexOf(file.type) !== -1if (!imgType) {this.$Message.warning({content:  '文件  ' + res.name + '  格式不正确, 请选择格式正确的图片',duration: 5});return false}// 控制文件上传大小let imgSize = localStorage.getItem('file_size_max');//获取缓存的文件大小限制字段let Maxsize = res.size  < imgSize;let fileMax = imgSize/ 1024 / 1024;if (!Maxsize) {this.$Message.warning({content: '文件体积过大,图片大小不能超过' + fileMax + 'M',duration: 5});return false}this.fileLists.push(file)//关键点return false},submitUpload() {//使用ref调用组件本身的post方法上传文件let _this = thisthis.fileLists.forEach(n => {_this.$refs.upload.post(n)})}}
}

本次组件分享完毕,欢迎小伙伴组团交流~

http://www.lryc.cn/news/135494.html

相关文章:

  • 无涯教程-PHP - File 函数
  • elasticsearch 常用查询 7.4 版本
  • ChatGpt 从入门到精通
  • vscode远程调试
  • Vue3 数据响应式原理
  • 2023.08.20 学习周报
  • 软件测试技术之单元测试—工程师 Style 的测试方法(2)
  • 项目中超图 for openlayer和超图for cesium同时引入的问题
  • 3D与沉浸式技术,如何助力企业数字化转型?
  • excel vba 将多张数据表的内容合并到一张数据表
  • 接口和抽象类的区别?解析接口和抽象类的特点和用法
  • vscode-vue项目格式化
  • SAP MM学习笔记26- SAP中 振替转记(转移过账)和 在库转送(库存转储)1- 移动Type间振替转记
  • SAP SPL(Special Ledger)之注释行项目-Noted Items
  • 学习平台助力职场发展与提升
  • 有没有免费格式转换工具推荐?PDF转化为PPT的方法
  • 【LeetCode-经典面试150题-day12】
  • TCP机制-延迟应答,捎带应答
  • 【Redis从头学-8】Redis中的ZSet数据类型实战场景之用户积分榜
  • Springboot内嵌SQLite配置使用
  • 【微服务学习笔记】认识微服务
  • 基于Android R快速编译recovery-ramdisk.img
  • Redis分布式缓存
  • 最大公约数和最小公倍数
  • 数据结构——二叉搜索树(附带C++实现版本)
  • C++(3)C++对C的扩展Extension
  • 在vscode(idea)使用GitHub账号、Copilot异常
  • 新的后端渲染:服务器驱动UI
  • Postman如何做接口自动化测试?
  • excel文本函数篇2