Update index.tsx

Made modification to make the system support uploading a folder so that the system recursively retrieves all files within the folder and sequentially adds them to the upload list. Add a button to upload the folder.
By the way, remember to modify the Knowledge Configuration and NGINX_CLIENT_MAX_BODY_SIZE in the file named .env.example in main menu to ensure the uploading is allowed for size limitation.
pull/17026/head^2
Silow 1 year ago committed by GitHub
parent d565802ea1
commit 1680e6527a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -47,6 +47,10 @@ const FileUploader = ({
const dropRef = useRef<HTMLDivElement>(null) const dropRef = useRef<HTMLDivElement>(null)
const dragRef = useRef<HTMLDivElement>(null) const dragRef = useRef<HTMLDivElement>(null)
const fileUploader = useRef<HTMLInputElement>(null) const fileUploader = useRef<HTMLInputElement>(null)
// For upload folder
const dirUploader = useRef<HTMLInputElement>(null)
const hideUpload = notSupportBatchUpload && fileList.length > 0 const hideUpload = notSupportBatchUpload && fileList.length > 0
const { data: fileUploadConfigResponse } = useSWR({ url: '/files/upload' }, fetchFileUploadConfig) const { data: fileUploadConfigResponse } = useSWR({ url: '/files/upload' }, fetchFileUploadConfig)
@ -214,6 +218,12 @@ const FileUploader = ({
fileUploader.current.click() fileUploader.current.click()
} }
// click on button, use folder upload
const selectFolderHandle = () => {
if (dirUploader.current)
dirUploader.current.click()
}
const removeFile = (fileID: string) => { const removeFile = (fileID: string) => {
if (fileUploader.current) if (fileUploader.current)
fileUploader.current.value = '' fileUploader.current.value = ''
@ -223,7 +233,15 @@ const FileUploader = ({
} }
const fileChangeHandle = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { const fileChangeHandle = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const files = [...(e.target.files ?? [])] as File[] const files = [...(e.target.files ?? [])] as File[]
initialUpload(files.filter(isValid)) const validFiles = files.filter(isValid)
initialUpload(validFiles)
}, [isValid, initialUpload])
// Callback for folder upload
const folderChangeHandle = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const files = [...(e.target.files ?? [])] as File[]
const validFiles = files.filter(isValid)
initialUpload(validFiles)
}, [isValid, initialUpload]) }, [isValid, initialUpload])
const { theme } = useTheme() const { theme } = useTheme()
@ -245,6 +263,7 @@ const FileUploader = ({
return ( return (
<div className="mb-5 w-[640px]"> <div className="mb-5 w-[640px]">
{!hideUpload && ( {!hideUpload && (
<>
<input <input
ref={fileUploader} ref={fileUploader}
id="fileUploader" id="fileUploader"
@ -254,6 +273,26 @@ const FileUploader = ({
accept={ACCEPTS.join(',')} accept={ACCEPTS.join(',')}
onChange={fileChangeHandle} onChange={fileChangeHandle}
/> />
{/* folder uploader */}
<input
ref={dirUploader}
className="hidden"
type="file"
webkitdirectory="true"
directory=""
onChange={folderChangeHandle}
/>
</>
)}
{/* give user a “Upload Folder” button */}
{!hideUpload && (
<button
onClick={selectFolderHandle}
className="mb-2 rounded bg-secondary px-4 py-2 text-black"
>
Upload Folder
</button>
)} )}
<div className={cn('mb-1 text-sm font-semibold leading-6 text-text-secondary', titleClassName)}>{t('datasetCreation.stepOne.uploader.title')}</div> <div className={cn('mb-1 text-sm font-semibold leading-6 text-text-secondary', titleClassName)}>{t('datasetCreation.stepOne.uploader.title')}</div>

Loading…
Cancel
Save