首页 > 其他分享 >AntDesignVue 通过点击确定按钮实现文件上传

AntDesignVue 通过点击确定按钮实现文件上传

时间:2023-10-23 17:26:51浏览次数:25  
标签:const AntDesignVue type value fileList file 按钮 import 上传

示例图

相关代码

<template>
    <div>
        <a-modal
                v-model:visible="props.uploadVisible"
                width="1300px"
                :footer="null"
                maskClosable
                @cancel="onCancelClick"
        >
            <div class="upload-box-container">
                <div class="upload-box">
                    <span class="title-box">xxxx</span>
                    <a-upload-dragger
                            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                            v-model:file-list="dealFileList"
                            name="file"
                            :multiple="false"
                            :action="actionTarget"
                            :before-upload="(e) => beforeUpload(e, 'deal')"
                            @change="handleFileChange"
                    >
                        <p class="ant-upload-drag-icon">
                            <InboxOutlined/>
                        </p>
                        <p class="ant-upload-text">点击上传文件或者拖拽上传</p>
                        <p class="ant-upload-hint" style="z-index: 100">
                            <a-button type="link" @click.stop="handleTemplateDownload('xxxxx', 'T05')" style="font-weight: 400;">
                                <LoadingOutlined v-if="dealBtnLoading"/>下载模板
                            </a-button>
                        </p>
                    </a-upload-dragger>
                </div>
                <div class="upload-box">
                    <span class="title-box">xxxxx</span>
                    <a-upload-dragger
                            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                            v-model:file-list="topicFileList"
                            name="file"
                            :multiple="false"
                            :action="actionTarget"
                            :before-upload="(e) => beforeUpload(e, 'topic')"
                            @change="handleFileChange"
                    >
                        <p class="ant-upload-drag-icon">
                            <InboxOutlined/>
                        </p>
                        <p class="ant-upload-text">点击上传文件或者拖拽上传</p>
                        <p class="ant-upload-hint" style="z-index: 100">
                            <a-button type="link" @click.stop="handleTemplateDownload('xxxxxx', 'T04')" style="font-weight: 400;">
                                <LoadingOutlined v-if="topicBtnLoading"/>下载模板
                            </a-button>
                        </p>
                    </a-upload-dragger>
                </div>
                <div class="upload-box">
                    <span class="title-box">账户信息</span>
                    <a-upload-dragger
                            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                            v-model:file-list="accountFileList"
                            name="file"
                            :multiple="false"
                            :action="actionTarget"
                            :before-upload="(e) => beforeUpload(e, 'account')"
                            @change="handleFileChange"
                    >
                        <p class="ant-upload-drag-icon">
                            <InboxOutlined/>
                        </p>
                        <p class="ant-upload-text">点击上传文件或者拖拽上传</p>
                        <p class="ant-upload-hint" style="z-index: 100">
                            <a-button type="link" @click.stop="handleTemplateDownload('xxxxx', 'T03')" style="font-weight: 400;">
                                <LoadingOutlined v-if="accountBtnLoading"/>下载模板
                            </a-button>
                        </p>
                    </a-upload-dragger>
                </div>
            </div>
            <div class="modal-btn">
                <a-space style="margin-top: 40px;display: flex; justify-content: flex-end">
                    <a-button @click="onCancelClick">取消</a-button>
                    <a-button type="primary" @click="handleSubmit" :loading="state.btnLoading">
                        提交
                    </a-button>
                </a-space>
            </div>
        </a-modal>
    </div>
</template>

<script setup>
import {defineProps, reactive, ref, watch} from "vue";
import {message} from "ant-design-vue";
import {InboxOutlined, LoadingOutlined} from "@ant-design/icons-vue";
import {useRoute} from "vue-router";
import mitt from "@/utils/mitt";
import axios from "axios";
import {templateDownload} from "@/api/biz/common";
import {doDownload} from "@/utils/common";

const route = useRoute();

const props = defineProps({
    uploadVisible: {
        type: Boolean,
        default: false,
    },
});

watch(() => props.uploadVisible, () => {
    dealFileList.value = [];
    topicFileList.value = [];
    accountFileList.value = [];
}, {deep: true})


const emits = defineEmits(['changeFundChangeUploadVisible']);
const importFileList = ref([]);
const actionTarget = ref('');

const dealBtnLoading = ref(false);
const topicBtnLoading = ref(false);
const accountBtnLoading = ref(false);

const state = reactive({
    btnLoading: false,
    fileList: [],
})

const dealFileList = ref([]); // 交易明细
const topicFileList = ref([]); // 主题信息
const accountFileList = ref([]);  // 账户信息
const handleSubmit = async () => {
    if (!dealFileList.value) {
        message.warn('请上传xxxx!')
        return;
    }
    if (!topicFileList.value) {
        message.warn('请上传xxxx!')
        return;
    }
    if (!accountFileList.value) {
        message.warn('请上传xxxx!')
        return;
    }
    if (dealFileList.value && topicFileList.value && accountFileList.value) {
        state.btnLoading = true;
        const [res01, res02, res03] = await Promise.all([
            handleImportFile(dealFileList.value, '/economic/economyDeal/import'),
            handleImportFile(topicFileList.value, '/economic/economyMain/import'),
            handleImportFile(accountFileList.value, '/economic/economyAccount/import'),
        ]).catch((error) => {
            console.log('output-> error::: ', error)
        })
        if (res01 && res02 && res03) {
            state.btnLoading = false;
            onCancelClick();
            resetFile();
            message.success('文件上传成功')
        }
    }
}

const resetFile = () => {
    dealFileList.value = [];
    topicFileList.value = [];
    accountFileList.value = [];
}
const handleImportFile = (file, requestPath) => {
    return new Promise((resolve, reject) => {
        let param = new FormData()
        param.append('file', file)
        axios.post(requestPath, param, {
            'Content-type': 'multipart/form-data'
        })
            .then(() => {
                resolve(true)
            }).catch(() => {
            reject(false)
        })
    })
}

const beforeUpload = (file, type) => {
    console.log('output-> type::: ', type)
    let fileList;
    if (type === 'deal') {
        fileList = dealFileList;
    }
    if (type === 'topic') {
        fileList = topicFileList;
    }
    if (type === 'account') {
        fileList = accountFileList;
    }
    fileList.value = null;
    // 判断文件类型
    const isJpgOrPng =
        file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
        file.type === 'application/vnd.ms-excel';
    if (!isJpgOrPng) {
        message.warning('仅允许 xls 或  xlsx 文件类型!');
        return;
    }
    console.log('output-> file', file)
    fileList.value = file;
    return false;
};


const handleTemplateDownload = async (name, code) => {
    let btnLoading = undefined;
    if(code === 'T05') {
        btnLoading = dealBtnLoading
    }
    if(code === 'T04') {
        btnLoading = topicBtnLoading
    }
    if(code === 'T03') {
        btnLoading = accountBtnLoading
    }
    btnLoading.value = true
    const res = await templateDownload({code: code})
    let param = {
        address: res.data.data,
        name: name,
        extName: 'xlsx'
    }
    doDownload(param)
    btnLoading.value = false
}
const onCancelClick = () => {
    emits('changeFundChangeUploadVisible')
}

const handleDownload = () => {
    emits('templateDownload')
}
const handleFileChange = (e) => {
    let fileList = [...e.fileList]
    fileList = fileList.map(file => {
        if (file.response) {
            console.log('output-> file.response:::: ', file.response)
            if (file.response.message === 'success') {
                importFileList.value = [];
                mitt.emit('emitResetForm');
                message.success('操作成功')
                onCancelClick();
                return;
            } else {
                importFileList.value = [];
                mitt.emit('emitResetForm');
                return;
            }
        }
        return file
    })
}

</script>

<style lang="scss" scoped>
:deep(.ant-upload .ant-upload-drag .ant-upload-btn) {
  top: 18px;
  height: 220px;
}

.upload-box-container {
  display: flex;
  justify-content: space-between;

  padding-left: 20px;

  .upload-box {
    flex: 1;
    height: 320px;
    margin-right: 15px;
    position: relative;
    top: 20px;
    margin-bottom: 60px;
  }
}

.title-box {
  margin-bottom: 15px;
  display: flex;
  justify-content: center;
  font-weight: 800;
  color: var(--primary-900);
}


</style>

标签:const,AntDesignVue,type,value,fileList,file,按钮,import,上传
From: https://www.cnblogs.com/openmind-ink/p/17782957.html

相关文章

  • java上传文件到服务器指定目录
    问题描述:如何在Java中实现文件上传功能,并将上传的文件保存到服务器的指定目录?解答:在Java中,可以使用一些库和框架来实现文件上传功能,其中最常用的是使用ApacheCommonsFileUpload库。下面将详细介绍如何使用该库来实现文件上传并将文件保存到服务器指定目录的过程。步骤一:导入......
  • git push使用LFS上传超过100M的文件,亲测有效
     一、问题描述使用gitpush将一个173.86MB的文件推送到GitHub时出现如下报错remote:error:Trace:5c39a1831dc9eced8723579b000596bbbeb91a9069931bbdf49b058aaaf1f64cremote:error:Seehttps://gh.io/lfsformoreinformation.remote:error:Filelinux-zero-......
  • TestLink上传xml文件报错:错误的测试用例集xml文件
    将测试用例转化为xml文件后,上传,总是报错: 仔细观察理解后发现,原来TestLink分两种:测试用例集和测试用例: 测试用例集相关的编辑、导入等,通过第一行来进行;而测试用例则通过第二行进行操作,弄清楚原因后,选中下面那个导入,再试一下: 果然导入成功了 ......
  • java上传文件到指定服务器
    Java上传文件到指定主机必须条件先决条件是要知道上传到指定的主机,需要知道它的用户密码。代码pom.xml<!--图片上传到服务器需要的依赖--><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.54<......
  • 文件上传请求头设置
    文件上传请求头设置涉及到文件上传时接口需要配置请求头:headers:{‘Content-Type’:‘multipart/form-data’}例如://导入excel数据exportfunctionimportStorage(data){returnrequest({url:'/agcloud/zhps/storage/importFile',method:'p......
  • 老大加需求:做一个支持超大文件 HTTP 断点续传的上传服务,我懵逼了~
    作者:大飞飞鱼来源:blog.csdn.net/ababab12345/article/details/80490621Part1前言最近由于笔者所在的研发集团产品需要,需要支持高性能的大文件(大都数是4GB以上)的http上传,并且要求支持http断点续传。笔者在以前的博客如何实现支持大文件的高性能HTTP文件上传服务器已经介绍了......
  • js实现大文件分片上传的方法
    借助js的Blob对象FormData对象可以实现大文件分片上传的功能,关于Blob和FormData的具体使用方法可以到如下地址去查看FormData对象的使用Blob对象的使用以下是实现代码,本例中后端代码使用php来实现,只是演示基本功能,具体一些文件验证逻辑先忽略。前段代码:<!DOCTYPEhtml><htmlla......
  • MinIO实现文件上传与下载
    引入依赖<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>7.1.0</version></dependency> 工具类 MinIoUtils@AutowiredSiMinIoP......
  • uniCloud cms 自媒体资讯新闻文章应用系统 uniapp+uniCloud+AntDesignVue Life cms
    介绍LifeCMS是uniCloud+uni-app云端一体全套CMS/自媒体/资讯/新闻/文章应用系统,前台包含注册、登录(账号密码登录、短信登录、微信手机号快捷登录、微信一键登录、App手机一键登录、Apple登录)、文章列表、文章详情、搜索、广告、分享、评论、回复、点赞、收藏、用户中心、意见......
  • js 大文件切片,中止上传,上传进度,断点续传
    大文件切片上传背景介绍:当涉及大文件上传时,一种有效的方法是将大文件分割成小切片并逐个上传。这种技术不仅可以减轻服务器的负担,还可以避免上传过程中的中断和内存问题。本文将介绍如何使用JavaScript实现大文件切片上传,并解释如何处理断点续传、并发控制以及上传取消等问题,用到......