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>
相关推荐
DFT计算杂谈3 分钟前
AMSET 设置多核并行计算
java·前端·css·html·css3
花椒技术7 分钟前
AI 协同开发落地复盘:1 小时生成首版后,为什么 Review 和修正又花了 2-3 天
前端·人工智能·架构
万少41 分钟前
万少用9个AI工具,帮朋友完成了一个"不可能"的项目
前端
小小小小宇43 分钟前
Vue `import` 为什么可以异步加载
前端
WMYeah1 小时前
【无标题】
前端·rust·抽奖程序·跨平台抽奖程序
Unbelievabletobe1 小时前
免费外汇api的响应时间在不同时段下的波动分析
大数据·开发语言·前端·python
大哥,带带弟弟1 小时前
Grafana 前端嵌入与 JWT 鉴权实战
前端·grafana
小小小小宇1 小时前
前端 V8 引擎垃圾回收机制与内存问题排查
前端
前端老石人1 小时前
CSS 值定义语法
前端·css
sheeta19981 小时前
Vue 前端基础笔记
前端·vue.js·笔记