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

news/2024/7/10 2:54:28 标签: elementui, iview, vue

今天要分享的是使用这俩个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) !== -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)
			})
        }
    }
}

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


http://www.niftyadmin.cn/n/4960842.html

相关文章

【面试题】前端面试复习6---性能优化

前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;前端面试题库 性能优化 一、性能指标 要在 Chrome 中查看性能指标&#xff0c;可以按照以下步骤操作&#xff1a; 打开 Chrome 浏览器&#xff0c;并访问你想要测试…

Linux: 使用 ssh 连接其他服务器

通过ifconfig 查看要连接的服务器地址&#xff1a; ubuntuubuntu1804-0172:/media/sangfor/vdc$ ssh ubuntu192.168.11.49 输入要连接的服务器密码: ubuntua192.168.1149 s password: 连接服务器成功&#xff1a;

Java请求webservice踩过的坑

最近项目对接过程中&#xff0c;因为对方系统比较旧&#xff0c;我们和对方进行交互使用webservice方式进行&#xff0c;对方给出相关文档&#xff0c; 接口地址&#xff1a;http://ip:port/abc/def/xxxService?wsdl 接口名称&#xff1a;methodA 1-springboot配合CXF使用 …

redis五大类型分析--list(2)

此篇为对redis五大数据类型中list的分析&#xff0c;希望能有所帮助 List API listTypeGet函数 robj *listTypeGet(listTypeEntry *entry) {robj *value NULL;/* 检查编码类型是否为 quicklist (快速列表) */if (entry->li->encoding OBJ_ENCODING_QUICKLIST) {/* 元素…

APP外包开发上线问题解决

在APP上线过程中可能会遇到各种问题&#xff0c;这些问题可能涉及技术、审核、发布等方面。可能会遇到各种挑战&#xff0c;但通过充分的准备、测试和持续的改进&#xff0c;可以解决大部分问题并提升应用的质量和用户满意度。以下是一些常见的问题及其解决方案&#xff0c;希望…

pdf转ppt在线的好工具有什么推荐?pdf转ppt方法介绍

在某些情况下&#xff0c;我们可能需要对PPT文件进行编辑、修改或重新排版&#xff0c;以满足特定的演讲需求或观众的需求。通过将PDF转换为PPT&#xff0c;我们可以方便地对文本、图片、布局等进行修改&#xff0c;使得演示内容更加贴合我们的目标&#xff0c;更能有效地传达我…

Whisper.cpp 编译使用

Whisper.cpp 编译使用 whisper.cpp 是牛人 ggerganov 对 openai 的 whisper 语音识别模型用 C 重新实现的项目&#xff0c;开源在 github 上&#xff0c;具有轻量、性能高&#xff0c;实用性强等特点。这篇文章主要记录在 windows 平台&#xff0c;如何使用该模型在本地端进行…

数据结构,队列,顺序表队列,链表队列

队列是一种常见的数据结构&#xff0c;它具有先进先出&#xff08;First-In-First-Out&#xff0c;FIFO&#xff09;的特性&#xff0c;类似于排队等候的场景。以下是队列的要点&#xff1a; 1. 定义&#xff1a;队列是一种线性数据结构&#xff0c;由一系列元素组成&#xff…