Ant Design Vue和VUE3下的upload组件使用以及文件预览

在这里插入图片描述

Ant Design Vue和VUE3下的upload组件使用以及文件预览

用到技术:Ant Design Vue、VUE3、xlsx的文件预览功能(也可预览txt,csv)

一、多文件上传

1.需求

  1. 可以多文件上传
  2. 文件先上传到本地,点击开始上传再通过后端接口继续上传
  3. 上传后显示全部、正确和错误的数据信息
  4. 上传后可以下载

注意细节:

  1. 最开始文件上传到本地时,右边页面不加载,当点击上传后,在右边未获取数据之前,
    a.上传组件无法再次上传;
    b:右边数据均处于加载中状态;
    c.文件下载处于不可编辑状态
  2. 加载出数据后,
    a.上传组件可再次上传;
    b.文件预览中的全部、正确和错误数据均需显示出对应的数据序号
    c.正确和错误的数据,均不显示‘比对结果’,仅全部数据显示
  3. 文件下载接口注意文件类型为‘blob’

2.样例

在这里插入图片描述

3.代码

<template>
	<a-row :gutter="8">
		<a-col :span="8">
			<a-card title="文件上传">
				<a-upload-dragger
					v-model:fileList="fileList"
					name="files"
					accept=".txt"
					:multiple="true"
					:action="action"
					:before-upload="beforeUpload"
					:disabled="upLoadDisabled"
					@remove="handleRemove"
					:showUploadList="{
   
						showRemoveIcon: true
					}"
				>
					<p class="ant-upload-drag-icon">
						<inbox-outlined></inbox-outlined>
					</p>
					<p class="ant-upload-text">点击或将文件拖拽到这里上传, 支持扩展名:.txt</p>
				</a-upload-dragger>
				<div style="text-align: right">
					<a-button
						type="primary"
						:disabled="fileList.length === 0"
						:loading="uploading"
						style="margin-top: 16px"
						@click="handleUpload"
					>
						{
   {
    uploading ? '上传中' : '开始上传' }}
					</a-button>
				</div>
			</a-card>
		</a-col>
		<a-col :span="16">
			<a-card>
				<a-spin :spinning="upLoadSpinning" tip="数据加载中...">
					<a-row :gutter="16">
						<a-col :span="12">
							<a-statistic title="正确/错误(个)" :value="trueNum" class="demo-class">
								<template #suffix>
									<span>/{
   {
    falseNum }}</span>
								</template>
							</a-statistic>
						</a-col>
						<a-col :span="12">
							<a-statistic title="完善度" :value="wcdPercent" style="margin-right: 50px" />
						</a-col>
					</a-row>
				</a-spin>
			</a-card>
			<a-card title="文件预览" style="margin-top: 5px">
				<a-spin :spinning="upLoadSpinning" tip="数据加载中...">
					<a-tabs v-model:activeKey="activeKey" type="card" @change="tabChange">
						<a-tab-pane key="0">
							<template #tab>
								<div>全部</div>
							</template>
							<div style="padding-bottom: 20px; padding-top: 20px">
								<div v-if="tableData0.length > 0">
									<a-table :columns="columns" :data-source="tableData0" bordered> </a-table>
								</div>
								<div v-else>
									<div class="emptyStyle"></div>
								</div>
							</div>
						</a-tab-pane>
						<a-tab-pane key="1">
							<template #tab>
								<div>正确</div>
							</template>
							<div style="padding-bottom: 20px; padding-top: 20px">
								<div v-if="tableData1.length > 0">
									<a-table :columns="columns1" :data-source="tableData1" bordered> </a-table>
								</div>
								<div v-else>
									<div class="emptyStyle"></div>
								</div>
							</div>
						</a-tab-pane>
						<a-tab-pane key="2">
							<template #tab>
								<div>错误</div>
							</template>
							<div style="padding-bottom: 20px; padding-top: 20px">
								<div v-if="tableData2.length > 0">
									<a-table :columns="columns1" :data-source="tableData2" bordered> </a-table>
								</div>
								<div v-else>
									<div class="emptyStyle"></div>
								</div>
							</div>
						</a-tab-pane>
						<template #rightExtra>
							<a-button @click="fileDownloadBtn" type="primary" :disabled="fileDownload">文件下载</a-button>
						</template>
					</a-tabs>
				</a-spin>
			</a-card>
		</a-col>
	</a-row>
</template>

<script setup>
	import {
    message } from 'ant-design-vue'
	import sysConfig from '@/config'
	import uploadApi from '@/api/auth/uploadApi'
	import * as XLSX from 'xlsx'
	import {
    clone } from 'lodash-es'
	import fileApi from '@/api/dev/fileApi'
	const props = defineProps({
   
		
你可以使用Ant Design的上传组件,在Vue 3中进行父组件调用。首先,你需要在父组件引入Ant DesignUpload组件: ``` <template> <div> <a-upload :action="uploadUrl" :headers="headers" :data="data" :multiple="multiple" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="onSuccess" :on-error="onError" :disabled="disabled" > <a-button> <a-icon type="upload"></a-icon> Click to Upload </a-button> </a-upload> </div> </template> <script> import { defineComponent } from &#39;vue&#39;; import { Upload, Button, Icon } from &#39;ant-design-vue&#39;; export default defineComponent({ name: &#39;ParentComponent&#39;, components: { &#39;a-upload&#39;: Upload, &#39;a-button&#39;: Button, &#39;a-icon&#39;: Icon }, data() { return { uploadUrl: &#39;https://www.example.com/upload&#39;, headers: {}, data: {}, multiple: true, disabled: false }; }, methods: { beforeUpload() { // Do something before upload }, onProgress() { // Do something on progress }, onSuccess() { // Do something on success }, onError() { // Do something on error } } }); </script> ``` 在父组件中,你需要设置上传组件的一些属性,例如上传的URL、请求头、数据、是否允许多文件上传、是否禁用等等。你还需要定义一些上传的回调函数,例如在上传之前执行的函数、上传进度更新函数、上传成功回调函数、上传失败回调函数等等。 在模板中,你可以使用`<a-upload>`标签来引用Ant Design的上传组件。你还可以使用`<a-button>`标签`<a-icon>`标签来设置上传按钮的样式。 在子组件中,你可以使用`$emit`函数来触发父组件中定义的回调函数。例如,在上传成功时,你可以使用以下代码触发`onSuccess`函数: ``` this.$emit(&#39;on-success&#39;); ``` 在父组件中,你可以使用`@`符号来绑定子组件触发的事件。例如,你可以使用以下代码来绑定上传成功事件: ``` <a-upload ... @on-success="onSuccess" > ``` 这样,当子组件触发上传成功事件时,父组件中定义的`onSuccess`函数就会被调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coisini_甜柚か

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值