编程随笔 编程随笔
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

  • 代码调试

  • vue2

  • vue3

    • 注意事项
    • swagger自动生成接口
    • 动态路由
    • 整合 Element Plus
    • wangeditor使用
    • monacoEditor使用
    • 自定义404页面
    • 验证码封装
    • 联表查询SQL
    • element-plus多文件手动上传
  • react

  • typescript

  • 前端
  • vue3
华总
2024-11-03
0
0

element-plus多文件手动上传原创

提示

http-request上传多文件会导致每个文件都会自动上传从而多次请求后端接口,设置auto-upload=false,然后再添加一个上传按钮来请求后端接口,只提交一次上传多个文件,上传成功后清除上传文件列表

接口文件

/**
 * 上传多个文件
 */
export function multipartUploadLicense(files: File[], onProgress: (progressEvent: ProgressEvent) => void ): AxiosPromise<FileInfo> {
  const formData = new FormData();
	files.forEach((file) => {
			formData.append('files', file);
	});
	//@ts-ignore
	return request({
		url: "/api/license/multipartUpload",
		method: "post",
		data: formData,
		onUploadProgress: onProgress,  // 设置上传进度回调
		headers: {
			"Content-Type": "multipart/form-data",
		},
	});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

vue文件

<template>
	<div>
		<el-upload
			ref="uploadRef"
			v-model:file-list="fileList"
			class="upload-demo"
			drag
			action=""
			accept=".lic, .keystore"
			:before-upload="beforeUpload"
			:on-change="handleChange"
			multiple
			:limit="2"
			:on-exceed="handleExceed"
			:auto-upload=false
			style="width: 50%;margin: 20px auto"
		>
			<el-icon class="el-icon--upload"><upload-filled /></el-icon>
			<div class="el-upload__text">
				拖拽或<em>点击上传</em>
			</div>
			<template #tip>
				<div class="el-upload__tip" style="color:red">
					只能上传.lic和.keystore等授权文件,不能超过2个,每个大小不超2M
				</div>
			</template>
		</el-upload>

		//进度条
		<el-progress
			v-if="uploading"
			:percentage="uploadProgress"
			:text-inside="true"
			:stroke-width="24"
			status="success"
			style="width: 80%;margin: 0 auto"
		/>

		<el-button type="primary" style="width: 300px;margin: 20px auto; display: block" @click="handleUpload">上传授权文件</el-button>

		<el-descriptions title="授权文件信息" :column="1" border class="margin-top">
			<el-descriptions-item
				label="开始时间"
				label-align="right"
				align="center"
			>
				<el-tag size="small">{{ LicenseInfo.startTime }} </el-tag>
			</el-descriptions-item>
			<el-descriptions-item label="结束时间" label-align="right" align="center">
				<el-tag size="small">{{ LicenseInfo.endTime }} </el-tag>
			</el-descriptions-item>
			<el-descriptions-item label="用户数量" label-align="right" align="center">
				<el-tag size="small">{{ LicenseInfo.userNum }} </el-tag>
			</el-descriptions-item>
			<el-descriptions-item label="备注" label-align="right" align="center">
				<el-tag size="small">{{ LicenseInfo.remark }} </el-tag>
			</el-descriptions-item>
		</el-descriptions>

	</div>

</template>

<script setup lang="ts">

import { UploadFilled } from '@element-plus/icons-vue'
import { uploadLicense, multipartUploadLicense } from '@/api/file'
import { LicenseControllerService } from "@/generated"
import type { UploadProps, UploadRawFile, UploadUserFile, UploadInstance } from "element-plus";


let uploadRef=ref<UploadInstance>()
const fileList = ref<UploadUserFile[]>([])
const LicenseInfo = ref({
	startTime: '',
	endTime: '',
	userNum: '',
	remark: '',
})

// 上传状态和进度
const uploading = ref(false);
const uploadProgress = ref(0);

const handleExceed: UploadProps['onExceed'] = (files) => {
	ElMessageBox.alert(`当前限制选择 2 个文件,共选择了 ${files.length} 个文件,请重新选择`);
}

const beforeUpload: UploadProps['beforeUpload'] = (rawFile: UploadRawFile) => {
 if (rawFile.size / 1024 / 1024 > 2) {
		ElMessage.error('文件大小不能超过2MB')
		return false
	}

	const allowedExtensions = ['.lic', '.keystore']; // 允许的文件类型
	const fileExtension = rawFile.name.split('.').pop()?.toLowerCase();
	if (!fileExtension || !allowedExtensions.includes(`.${fileExtension}`)) {
		ElMessage.error('不支持的文件类型');
		return false;
	}

	return true
}

const handleChange: UploadProps['onChange'] = (uploadFile, uploadFiles) => {
	// 将上传的文件添加到 fileList 中
	fileList.value = uploadFiles.map(file => file as UploadUserFile);
};

function handleUpload() {
	// 提取所有文件的 raw 属性
	const files = fileList.value.map(file => file.raw as File);
	if(files.length === 0){
		ElMessage.error('请选择要上传的授权文件')
		return
	}
	// 上传文件
	multipartUploadLicense(files, (event: ProgressEvent) => {
		uploading.value = true; // 显示进度条
		uploadProgress.value = Math.round(event.loaded / event.total * 100); // 更新进度
		//防止进度条的进度和上传的对勾显示时机不一致
    if(uploadProgress.value > 98){
			uploadProgress.value = 99
		}
	}).then(() => {
		ElMessage({
			type: 'success',
			message: '上传成功',
			grouping: true,
		})

		uploadRef.value!.clearFiles()
		uploadProgress.value = 100;
		uploading.value = false; // 隐藏进度条
		uploadProgress.value = 0; // 重置进度
		loadLicense()

	})
}

function loadLicense(){
	LicenseControllerService.verifyLicense().then(res => {
		if(res.data && res.code === 0){
			LicenseInfo.value = res.data as any
		}
	})
}

onMounted(() => {
	loadLicense()
})

</script>


<style scoped>

.margin-top {
	width: 80%;
	margin: 50px auto;
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
上次更新: 2025/02/18 14:46:10
联表查询SQL
tsconfig.json详解与常用配置

← 联表查询SQL tsconfig.json详解与常用配置→

最近更新
01
jFlash使用 原创
03-24
02
中央仓库上传指南 原创
03-23
03
模板生成工具 原创
02-18
04
RTC实时时钟 原创
02-12
05
keil调试 原创
01-21
更多文章>
Copyright © 2023-2025 liyao52033  All Rights Reserved
备案号:鄂ICP备2023023964号-1    
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式