feat:自建 RAG 聊天应用,限制知识库文档格式,只允许上传(".pdf", ".docx", ".md", ".txt")

This commit is contained in:
PingChuan
2026-04-20 17:55:49 +08:00
parent 5bee9288b9
commit 5aa040c94e
2 changed files with 23 additions and 4 deletions
@@ -22,14 +22,16 @@ import {
Spin, Spin,
Tooltip, Tooltip,
Upload, Upload,
message,
} from 'antd'; } from 'antd';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import type { Segment } from '~/api/dify-dataset/type'; import type { Segment } from '~/api/dify-dataset/type';
import { useDocumentUpload } from '~/hooks/dify-dataset-manager/document-upload'; import { useDocumentUpload } from '~/hooks/dify-dataset-manager/document-upload';
import type { DocumentUploadProps, UploadedDocument } from '~/types/dify-dataset-manager/document-upload'; import type { DocumentUploadProps, UploadedDocument } from '~/types/dify-dataset-manager/document-upload';
import { SUPPORTED_FORMATS } from '~/types/dify-dataset-manager/document-upload'; import { SUPPORTED_ACCEPT, SUPPORTED_FILE_EXTENSIONS, SUPPORTED_FORMATS } from '~/types/dify-dataset-manager/document-upload';
const { Dragger } = Upload; const { Dragger } = Upload;
const MAX_FILE_SIZE_MB = 15;
/** /**
* 文档上传组件 * 文档上传组件
@@ -121,9 +123,24 @@ export default function DocumentUpload({
<Dragger <Dragger
fileList={fileList} fileList={fileList}
onChange={handleFileChange} onChange={handleFileChange}
beforeUpload={() => false} beforeUpload={(file) => {
const lowerName = file.name.toLowerCase();
const isSupported = SUPPORTED_FILE_EXTENSIONS.some((ext) => lowerName.endsWith(ext));
if (!isSupported) {
message.error(`仅支持上传 ${SUPPORTED_FORMATS} 格式的文件`);
return Upload.LIST_IGNORE;
}
const isWithinLimit = file.size / 1024 / 1024 <= MAX_FILE_SIZE_MB;
if (!isWithinLimit) {
message.error(`单个文件大小不能超过 ${MAX_FILE_SIZE_MB}MB`);
return Upload.LIST_IGNORE;
}
return false;
}}
multiple={true} multiple={true}
accept=".txt,.md,.mdx,.pdf,.html,.htm,.xlsx,.xls,.docx,.csv,.vtt,.properties" accept={SUPPORTED_ACCEPT}
showUploadList={false} showUploadList={false}
> >
<p className="ant-upload-drag-icon"> <p className="ant-upload-drag-icon">
@@ -65,7 +65,9 @@ export const INDEXING_STATUS_CONFIG: Record<IndexingStatus, { text: string; perc
/** /**
* 支持的文件格式 * 支持的文件格式
*/ */
export const SUPPORTED_FORMATS = 'TXT, MARKDOWN, MDX, PDF, HTML, XLSX, XLS, DOCX, CSV, VTT, PROPERTIES, MD, HTM'; export const SUPPORTED_FILE_EXTENSIONS = ['.pdf', '.docx', '.md', '.txt'] as const;
export const SUPPORTED_FORMATS = 'PDF, DOCX, MD, TXT';
export const SUPPORTED_ACCEPT = SUPPORTED_FILE_EXTENSIONS.join(',');
/** /**
* 文档上传组件 Props * 文档上传组件 Props