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>
相关推荐
烤麻辣烫1 分钟前
Web开发概述
前端·javascript·css·vue.js·html
Front思11 分钟前
Vue3仿美团实现骑手路线规划
开发语言·前端·javascript
徐同保14 分钟前
Nano Banana AI 绘画创作前端代码(使用claude code编写)
前端
Ulyanov15 分钟前
PyVista与Tkinter桌面级3D可视化应用实战
开发语言·前端·python·3d·信息可视化·tkinter·gui开发
计算机程序设计小李同学15 分钟前
基于Web和Android的漫画阅读平台
java·前端·vue.js·spring boot·后端·uniapp
lkbhua莱克瓦2417 分钟前
HTML与CSS核心概念详解
前端·笔记·html·javaweb
沛沛老爹18 分钟前
从Web到AI:Agent Skills CI/CD流水线集成实战指南
java·前端·人工智能·ci/cd·架构·llama·rag
弓.长.26 分钟前
React Native 鸿蒙跨平台开发:i18n 国际化方案代码指南
react native·react.js·harmonyos
GISer_Jing26 分钟前
1.17-1.23日博客之星投票,每日可投
前端·人工智能·arcgis
代码游侠31 分钟前
学习笔记——ARM Cortex-A 裸机开发实战指南
linux·运维·开发语言·前端·arm开发·笔记