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

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

一、element upload组件传送门

1、html文件

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文件

javascript 复制代码
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文件

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文件

javascript 复制代码
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) !== -1
            if (!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 = this
			this.fileLists.forEach(n => {
				_this.$refs.upload.post(n)
			})
        }
    }
}

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

相关推荐
喜欢敲代码的程序员12 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
华子w90892585912 小时前
基于 SpringBoot+Vue.js+ElementUI 的 “花开富贵“ 花园管理系统设计与实现7000字论文
vue.js·spring boot·elementui
海的诗篇_14 小时前
前端开发面试题总结-原生小程序部分
前端·javascript·面试·小程序·vue·html
hackchen16 小时前
从0到1解锁Element-Plus组件二次封装El-Dialog动态调用
前端·vue.js·elementui
蓝胖子的多啦A梦18 小时前
Vue+element 日期时间组件选择器精确到分钟,禁止选秒的配置
前端·javascript·vue.js·elementui·时间选选择器·样式修改
夏天想18 小时前
vue2+elementui使用compressorjs压缩上传的图片
前端·javascript·elementui
海天胜景18 小时前
vue3 el-table 列增加 自定义排序逻辑
javascript·vue.js·elementui
sunbyte19 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DragNDrop(拖拽占用组件)
前端·javascript·css·vue.js·vue
满楼、10 天前
el-cascader 设置可以手动输入也可以下拉选择
javascript·vue.js·elementui
海天胜景10 天前
vue3 el-table 根据字段值 改变整行字体颜色
javascript·vue.js·elementui