当前位置: 首页 > news >正文

使用antd design pro 及后端nodejs express 结合minio进行文件的上传和下载管理

使用Ant Design Pro前端框架结合Node.js Express后端服务以及MinIO作为对象存储,实现文件上传和下载管理的基本步骤如下:

1. 安装所需依赖

在Node.js Express项目中安装minio客户端库:

npm install minio --save

在前端项目(假设基于Ant Design Pro)中安装文件上传组件依赖:

npm install @ant-design/pro-form axios --save

或者如果使用Uplader组件:

npm install antd-upload axios --save

2. 配置后端Express服务器与MinIO连接

创建一个名为minioClient.js的文件或在现有配置文件中添加以下内容以设置MinIO客户端:

// minioClient.js
const Minio = require('minio');const minioClient = new Minio.Client({endPoint: 'your-minio-server-address',port: 9000, // 或者你使用的端口useSSL: false, // 如果是HTTPS连接,则为trueaccessKey: 'your-access-key',secretKey: 'your-secret-key',
});module.exports = minioClient;

3. 创建文件上传API

在Express应用中创建一个路由处理文件上传至MinIO。

// server/controllers/minioController.js
const express = require('express');
const minioClient = require('../config/minioClient');const router = express.Router();router.post('/upload', async (req, res) => {try {const { file } = req.files; // 假设通过multer等中间件解析了文件// 创建一个新的对象名,可以包含路径前缀const objectName = `uploads/${file.originalname}`;// 上传文件到MinIOawait minioClient.putObject('your-bucket-name', objectName, file.buffer, file.size, 'application/octet-stream');res.status(200).json({ message: 'File uploaded successfully', url: `/download/${encodeURIComponent(objectName)}` });} catch (err) {console.error(err);res.status(500).json({ error: 'Failed to upload file' });}
});module.exports = router;// 在主应用中注册这个路由
// server/index.js or app.js
const minioController = require('./controllers/minioController');
app.use('/api', minioController);

4. 创建文件下载API

// 添加到上面的minioController.js
router.get('/download/:filename', async (req, res) => {try {const filename = decodeURIComponent(req.params.filename);const stream = await minioClient.getObject('your-bucket-name', filename);res.setHeader('Content-Type', 'application/octet-stream');res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);stream.pipe(res);} catch (err) {console.error(err);res.status(404).json({ error: 'File not found' });}
});

5. 前端Ant Design Pro实现文件上传

在Ant Design Pro中使用ProFormUpload或其他上传组件进行文件上传:

import { ProFormUploadButton } from '@ant-design/pro-form';
import axios from 'axios';function FileUploader() {const onFinish = async (values) => {const formData = new FormData();formData.append('file', values.file as Blob);try {const response = await axios.post('/api/upload', formData); // 替换为你的上传接口地址console.log(response.data);} catch (error) {console.error(error);}};return (<ProFormUploadButton name="file" label="上传文件" action="/api/upload" max={1} onFinish={onFinish} />);
}export default FileUploader;

6. 实现文件下载链接

根据后端返回的下载URL,可以直接生成下载链接给用户点击。例如,在React组件内:

import { Button } from 'antd';function DownloadLink({ downloadUrl }) {return (<Button type="primary" href={downloadUrl} download>下载文件</Button>);
}// 使用时传递从后端获取的URL
<DownloadLink downloadUrl={`/api/download/${encodeURIComponent('example.pdf')}`} />

请确保替换上述代码中的占位符为实际的MinIO服务器地址、访问密钥、安全密钥、桶名称以及文件名,并相应地调整文件上传和下载处理逻辑以适应您的具体需求。同时,您可能需要使用像multer这样的中间件来处理上传的文件流。

http://www.lryc.cn/news/286380.html

相关文章:

  • Unity常用的优化技巧集锦
  • c++动态调用dll
  • 使用Python自动化操作手机,自动执行常见任务,例如滑动手势、呼叫、发送短信等等
  • E - Souvenir(图论典型例题)
  • 【SpringBoot篇】添加富文本编辑器操作
  • 前台vue配置
  • 牛客周赛 Round 18 解题报告 | 珂学家 | 分类讨论计数 + 状态DP
  • CentOS防火墙基本操作
  • Shell脚本的编程规范和变量类型
  • C++面试:跳表
  • 掌握C++20的革命性特性:Concepts
  • win11开机后频繁刷新桌面,任务栏不显示,文件资源管理器explorer报错ntdll.dll
  • 解决Git添加.gitignore文件后不生效的问题
  • Shell Linux学习笔记
  • kingbase常用SQL总结之锁等待信息
  • 「优选算法刷题」:长度最小的子数组
  • RuoYi-Cloud本地部署--详细教程
  • 如何优雅的发布一个 TypeScript 软件包?
  • 总结的太到位:python 多线程系列详解
  • 惬意上手Python —— 装饰器和内置函数
  • python 调用dll
  • docker里Java服务执行ping命令模拟流式输出
  • 代码随想录算法训练营第十三天| 239. 滑动窗口最大值 、347.前 K 个高频元素
  • 旋转花键的使用寿命与机械原理分析
  • 互联网摸鱼日报(2024-01-22)
  • CentOS 7 安装配置MySQL
  • Gold-YOLO(NeurIPS 2023)论文与代码解析
  • 多个coco数据标注文件合并
  • Kubernetes(K8S)拉取本地镜像部署Pod 实现类似函数/微服务功能(可设置参数并实时调用)
  • k8s使用ingress实现应用的灰度发布升级