react 基于Ant Desgin Upload简单实现导入 导出功能

效果图:

导入:

导出:

导入代码:

javascript 复制代码
    const propsConfig = {
    name: 'file',
    action: importDataExcelApi,   //后端接口
    headers: {
        authorization: 'authorization-text',
        loginUserId: sessionStorage.getItem('userLogin')
            ? JSON.parse(sessionStorage.getItem('userLogin')).userId : null
    },
    onChange(info) {
        if (info.file.status !== 'uploading') {
            console.log("++++++", info.file, "-------------" + info.fileList);
            console.log(info.file.response.success);
        }
        if (info.file.status === 'done') {
            message.success(`${info.file.name} 文件上传成功!`).then(r => {
            });
            importDataRef.current(); // 调用 importData 方法
            console.warn(sessionStorage.getItem('userLogin'))
        } else if (info.file.status === 'error') {
            // 不再触发 importDataExcelApi,因为文件格式有误
            message.error('文件格式有误,导入失败!').then(r => {
            });
        }
    },
};


const isUpload = () => {
        Modal.warn({
            title: '重新导入',
            content: (
                <div style={{width: '500px'}}>
                    <div style={{marginBottom: '20px'}}>
                        点击上传 会重新导入文件数据
                    </div>
                    <Upload
                        {...propsConfig}
                        direction="vertical" maxCount={1}
                        showUploadList={false}
                    >
                        <Button
                            onClick={againImport}
                            icon={<UploadOutlined/>}>
                            上传
                        </Button>
                    </Upload>
                </div>
            ),
            onOk() {
                // 在弹窗点击确认后执行的操作

            },
            okText: '确定', // 修改确定按钮的文字
            // style: {width: '1000px', height: '500px'}, // 设置宽度和高度
        });
    };


           <Button type="primary" onClick={isUpload} icon={<UploadOutlined/>}
                    >
                        重新导入
                    </Button>

导出代码

javascript 复制代码
                 


const config2 = {
    title: '导出配置人员信息',
    content: (
        <>
            <ReachableContext.Consumer>{() => `是否要导出配置人员信息`}</ReachableContext.Consumer>
        </>
    ),
}






    const exportData = () => {
        exportDataExcel(screeningDate).then((res) => {
            if (res.data === 'false') {
                message.error("导出失败").then(r => {})
                return;
            }
            console.log('Export response:', res);
            //设置下载文件类型为xlsx 不同的类型type也不一样,创建URL对象
            let url = window.URL.createObjectURL(new Blob([res],
                {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}))
            // 创建A标签
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            // 设置的下载文件文件名
            const fileName = "配置人员信息";
            // 触发点击方法
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            message.success("导出成功").then(r => {
            });

        });
    }




  <Button type="primary" style={{marginBottom: '30px'}}
                                        onClick={async () => {
                                            const confirmed = await modal.confirm(config2);
                                            if (confirmed) {
                                                // 调用另一个方法
                                                exportData();
                                            }
                                        }}
                                >
                                    导出
                                </Button>
相关推荐
竹林81812 分钟前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花28 分钟前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12271 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪2 小时前
Vue3-生命周期
前端
莪_幻尘2 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4532 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端
林瞅瞅3 小时前
Nuxt3 项目部署 Nginx 防盗链后特定 JS 文件 403 问题修复方案
前端
kyriewen3 小时前
别再每次都 Google 了:我整理了前端日常最常踩的 10 个 Git 坑,附速查表
前端·javascript·git
一颗奇趣蛋3 小时前
Web 视频开发完全指南:从入门到精通
前端
非洲农业不发达4 小时前
windows终端体验大升级,让你拥有macos级别的美化
前端·后端