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>
相关推荐
IT_陈寒28 分钟前
SpringBoot高并发优化:这5个被忽视的配置让你的QPS提升300%
前端·人工智能·后端
光影少年35 分钟前
css优化都有哪些优化方案
前端·css·rust
BillKu43 分钟前
npm 安装命令中关于 @ 的讲解,如:npm install @vue-office/docx vue-demi
前端·vue.js·npm
yangzhi_emo1 小时前
ES6笔记4
前端·笔记·es6
萌萌哒草头将军1 小时前
Node.js v24.8.0 新功能预览!🚀🚀🚀
前端·javascript·node.js
超人不会飛1 小时前
大模型应用 Vue H5 模板:快速落地流式交互与富文本渲染的开箱方案
前端·vue.js·github
用户458203153171 小时前
CSS无需JavaScript的交互效果实现
前端·css
影i1 小时前
在 Vue + Codemirror 中优雅回显 JSON
前端
奇怪的前端71 小时前
Alien-Signals 响应式系统
前端·vue.js
你单排吧1 小时前
Electron打包图标修改失败问题
前端