본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

FileUpload

원문 보기 위키 갱신

드래그앤드롭, 검증, 진행 상황 추적을 지원하는 스타일 없는 파일 업로드 컴포넌트예요.

출처: 문서

본문

기능 (Features)

  • 하위 컴포넌트 구성 API: Root, Trigger, Upload, Clear, Content, ItemGroup, Item, ItemPreview, ItemInfo, ItemName, ItemSize, ItemRemove
  • 자체 연결된 액션 컴포넌트: Trigger(다이얼로그 열기), Upload, Clear, ItemRemove는 컨텍스트를 읽고 수동 핸들러가 필요 없어요
  • onError / onChange 콜백을 통해 표면화되는 내장 파일 타입·크기·개수 검증
  • 시각적 하이라이트 피드백이 있는 드래그앤드롭 파일 선택
  • 진행 상황 추적을 갖춘 XMLHttpRequest 기반 업로드
  • 클라이언트 측 파일 처리를 위한 커스텀 업로드 핸들러 지원
  • 대기 중 파일과 업로드된 파일의 개별 추적
  • Item 컨텍스트가 중첩 하위 컴포넌트에 파일 데이터를 제공하는 조합 가능한 파일 항목 렌더링

사용법 (Usage)

import { FileUpload } from 'primereact/fileupload';
<FileUpload.Root>
    <FileUpload.Trigger>Choose</FileUpload.Trigger>
    <FileUpload.Upload>Upload</FileUpload.Upload>
    <FileUpload.Clear>Clear</FileUpload.Clear>
    <FileUpload.Content>
        <FileUpload.ItemGroup>
            <FileUpload.Item file={file} index={index}>
                <FileUpload.ItemPreview />
                <FileUpload.ItemInfo>
                    <FileUpload.ItemName />
                    <FileUpload.ItemSize />
                </FileUpload.ItemInfo>
                <FileUpload.ItemRemove />
            </FileUpload.Item>
        </FileUpload.ItemGroup>
    </FileUpload.Content>
</FileUpload.Root>

동작 (Behavior)

다형적 렌더링 (Polymorphic Rendering)

as를 사용해 하위 컴포넌트가 렌더링할 HTML 요소를 바꿀 수 있어요.

<FileUpload.Root as="section">
    <FileUpload.Content as="main"></FileUpload.Content>
</FileUpload.Root>

기본 요소: Root=div, Trigger=button, Upload=button, Clear=button, Content=div, ItemGroup=div, Item=div, ItemPreview=img, ItemInfo=div, ItemName=div, ItemSize=div, ItemRemove=button.

렌더 함수 자식 (Render Function Children)

Root는 상태, 메서드, 계산된 속성을 가진 컴포넌트 인스턴스에 접근할 수 있는 렌더 함수를 children으로 받아요.

<FileUpload.Root>
    {(instance) => (
        <>
            <FileUpload.Trigger>Choose Files</FileUpload.Trigger>
            <FileUpload.Upload>Upload</FileUpload.Upload>
            <FileUpload.Content>
                <FileUpload.ItemGroup>
                    {instance.state.files.map((file, index) => (
                        <FileUpload.Item key={file.name + file.size} file={file} index={index}>
                            <FileUpload.ItemPreview />
                            <FileUpload.ItemInfo>
                                <FileUpload.ItemName />
                                <FileUpload.ItemSize />
                            </FileUpload.ItemInfo>
                            <FileUpload.ItemRemove>Remove</FileUpload.ItemRemove>
                        </FileUpload.Item>
                    ))}
                </FileUpload.ItemGroup>
            </FileUpload.Content>
        </>
    )}
</FileUpload.Root>

Pass Through

<DocPTViewer name="fileupload-pt" components={['FileUploadRoot']} />

API

FileUploadRoot

Label Type Description
root FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
content FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the content's DOM element.
fileList FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file list's DOM element.
file FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file's DOM element.
fileThumbnail FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file thumbnail's DOM element.
fileInfo FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file info's DOM element.
fileName FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the fileName's DOM element.
fileSize FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the fileSize's DOM element.
fileActions FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file actions' DOM element.
fileRemove FileUploadRootPassThroughType<HTMLAttributes> Used to pass attributes to the file remove button's DOM element.

FileUploadTrigger

Label Type Description
root FileUploadTriggerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadUpload

Label Type Description
root FileUploadUploadPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadClear

Label Type Description
root FileUploadClearPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadContent

Attribute Value
data-highlight "true" when files are dragged over the zone
Label Type Description
root FileUploadContentPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemGroup

Label Type Description
root FileUploadItemGroupPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItem

Label Type Description
root FileUploadItemPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemPreview

Label Type Description
root FileUploadItemPreviewPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemInfo

Label Type Description
root FileUploadItemInfoPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemName

Label Type Description
root FileUploadItemNamePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemSize

Label Type Description
root FileUploadItemSizePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

FileUploadItemRemove

Label Type Description
root FileUploadItemRemovePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

FileUpload는 파일 선택을 위해 숨겨진 네이티브 input[type="file"] 요소를 사용해요. 선택 버튼이 이 입력을 프로그래밍 방식으로 트리거해요. 파일 썸네일은 장식 이미지임을 나타내기 위해 role="presentation"을 사용해요. 오류 메시지는 스크린 리더가 알려줄 수 있도록 보이는 텍스트로 렌더링돼요.

키보드 지원 (Keyboard Support)

Key Function
tab Moves focus between the choose, upload, cancel buttons and file remove buttons.
enter / space Activates the focused button. Opens the file dialog when the choose button is focused.

더 알아보기 (Learn more)