vue3+ts 使用el-upload上传excel文件并自定义发送请求

<template>
	<div class="upload-page">
		<el-upload action="" class="upload-demo" drag accept=".xlsx, .xls" :on-exceed="exceedFile" :on-error="handleError"
			:on-success="handleSuccess" :http-request="uploadExcel" :before-upload="beforeUPload" :show-file-list="true"
			:limit="1">
			<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">请上传 .xls , .xlsx 标准格式文件</div>
			</template>
		</el-upload>
	</div>
</template>
<script setup lang="ts">
import http from "@/utils/http";
import { ElMessage, ElMessageBox } from 'element-plus'
//上传文件之前先判断该文件是否是Excel文件
// 文件上传之前判断
const beforeUPload = (file: any) => {
	const isExcel =
		file.type === 'application/vnd.ms-excel' ||
		file.type ===
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
	const isLt2M = file.size / 1024 / 1024 < 20;
	if (!isExcel)
		ElMessageBox({
			title: '温馨提示',
			message: '上传文件只能是 xls / xlsx 格式!',
			type: 'warning',
		});
	if (!isLt2M)
		ElMessageBox({
			title: '温馨提示',
			message: '上传文件大小不能超过 20MB!',
			type: 'warning',
		});
	return isExcel && isLt2M;
};
// 文件数超出提示
const exceedFile = () => {
	ElMessage.warning('最多只能上传一个文件!');
};
// 上传错误提示
const handleError = () => {
	ElMessage.error('导入数据失败,请您重新上传!');
};

//上传成功提示
const handleSuccess = () => {
	ElMessage.success('导入数据成功!');
};
// 文件上传
const uploadExcel = async (param: any) => {
	let fileFormData = new FormData();
	fileFormData.append('file', param.file);
	//导入公用人事考勤数据
	await http({//这里的http就是普通的axios实例
		method: 'post',
		url: 'xxxx',
		headers: { "Content-Type": "multipart/form-data" },
		data: fileFormData
	})
};
</script>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我可以给你提供一些思路和代码示例。 首先,你可以使用 `el-upload` 组件的 `before-upload` 和 `action` 属性来实现自定义上传图片的功能。具体来说,`before-upload` 用于在上传图片之前进行一些处理,而 `action` 则指定了上传图片的地址。 其次,在使用 `typescript` 时,你需要定义上传图片的数据类型。在 `vue3` 中,你可以使用 `defineComponent` 函数来定义组件,并使用 `ref` 来引用组件中的数据。 下面是一个示例代码,供你参考: ``` <template> <el-upload class="upload-demo" :action="uploadUrl" :before-upload="beforeUpload" :show-file-list="false"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface UploadResponse { code: number; data: string; } export default defineComponent({ setup() { const uploadUrl = ref('your_upload_url'); const beforeUpload = (file: File) => { // 在上传之前处理图片 return true; }; const handleUploadSuccess = (response: UploadResponse) => { // 处理上传成功后的响应 }; return { uploadUrl, beforeUpload, handleUploadSuccess, }; }, }); </script> ``` 在这个示例中,我定义了一个 `UploadResponse` 接口来表示上传图片后的响应数据类型。在 `setup` 函数中,我使用 `ref` 来定义了 `uploadUrl` 变量,并将其传递给 `el-upload` 组件的 `action` 属性。同时,我还定义了 `beforeUpload` 和 `handleUploadSuccess` 函数来处理上传前和上传后的数据。 当然,这只是一个简单的示例,你还需要根据具体的需求进行代码的修改和优化。希望这些信息能够对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值