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

Upload

원문 보기 위키 갱신

Upload는 파일을 업로드할 때 쓰는 컴포넌트예요. 버튼 클릭부터 드래그 앤 드롭, 디렉터리 업로드, 업로드 전 검사와 커스텀 요청까지 폭넓게 커버하죠. 파일 목록 표시 형태도 text, image, image-card 중에 고를 수 있어요.

출처: 문서

본문

지연 시간이 문제가 되지 않는다면, 그냥 트럭에 하드디스크를 가득 싣고 다니면 되겠죠.

Demos

basic.vue
drag.vue
submit-manually.vue
controlled.vue
on-finish.vue
default-files.vue
before-upload.vue
image-style.vue
image-card-style.vue
custom-request.vue
custom-download.vue
abstract.vue
download.vue

API

Upload Props

이름 타입 기본값 설명 버전
abstract boolean false DOM 감싸는 요소를 두지 않을지 여부예요. image-card 타입에서는 지원하지 않아요.
accept string undefined 업로드 accept 타입이에요. accept를 참고하세요.
action string undefined 데이터를 제출할 URL이에요.
always-show-actions boolean false 각 파일 항목에 액션 버튼(삭제, 다운로드, 재시도, 미리보기)을 항상 표시해요. false면 호버할 때만 나타나요. 2.45.0
create-thumbnail-url (file: File | null, fileInfo: UploadSettledFileInfo) => (Promise<string> | string | undefined) undefined 파일 썸네일을 커스터마이즈해요. undefined를 반환하면 기본 썸네일 표시 로직을 따라가요. fileInfo 2.34.0
custom-request (options: UploadCustomRequestOptions) => void undefined 업로드 요청을 커스터마이즈해요. 타입은 UploadCustomRequestOptions를 참고하세요.
custom-download (file: FileInfo) => void undefined 다운로드 요청을 커스터마이즈해요. 2.41.1
data Object | ({ file: UploadFileInfo }) => Object undefined HTTP 요청 form data에 추가할 필드 데이터예요.
default-file-list Array<UploadFileInfo> [] 비제어 방식의 기본 파일 목록이에요.
default-upload boolean true 파일을 선택하자마자 바로 업로드할지 여부예요.
directory boolean false 디렉터리 업로드를 허용할지 여부예요. (파일 열기 대화상자에서 디렉터리만 선택할 수 있어요) 2.28.3
directory-dnd boolean false 디렉터리 드래그 앤 드롭을 허용할지 여부예요. (설정하지 않으면 기본으로 directory prop을 따라가요.) 2.28.3
disabled boolean false 업로드를 비활성화할지 여부예요.
file-list-class string undefined 파일 목록 영역의 클래스예요. 2.36.0
file-list-style Object undefined 파일 목록 영역의 스타일이에요.
file-list Array<UploadFileInfo> undefined 컴포넌트의 파일 목록이에요. 설정하면 컴포넌트가 제어(controlled) 방식으로 동작해요.
headers Object | ({ file: UploadFileInfo }) => Object undefined 요청에 추가할 HTTP 헤더들이에요.
input-props InputHTMLAttributes undefined 파일 input의 속성이에요. 2.24.2
image-group-props ImageGroupProps undefined upload 안의 n-image props예요. ImageGroup Props를 참고하세요. 2.24.0
is-error-state (xhr: XMLHttpRequest) => boolean undefined 응답이 에러 상태인지 확인해요. 2.29.1
list-type string 'text' 파일 목록의 내장 스타일이에요. text, image, image-card가 있어요.
max number undefined 업로드 파일 수 제한이에요.
method string 'POST' HTTP 요청 메서드예요.
multiple boolean false 여러 파일 선택을 허용할지 여부예요.
name string 'file' HTTP 요청 form data에서 파일의 필드 이름이에요.
render-icon (file: UploadSettledFileInfo) => VNodeChild undefined 파일 아이콘의 렌더 함수예요. list-type="image" 또는 list-type="image-card"일 때만 동작해요. 2.34.0
response-type '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' '' n-upload가 쓰는 XMLHttpRequest의 응답 타입(responseType)이에요. 2.33.3
should-use-thumbnail-url (file: UploadSettledFileInfo) => boolean 이미지 타입 파일에만 true를 반환하는 함수. 파일의 썸네일을 표시할지 결정하는 함수예요. list-type="image" 또는 list-type="image-card"일 때만 동작해요. 2.34.0
show-cancel-button boolean true 취소 버튼을 표시해요(업로드 중). 이 이벤트에는 on-remove 콜백을 사용해요.
show-download-button boolean false 다운로드 버튼을 표시해요(업로드가 끝난 뒤).
show-file-list boolean true 파일 목록을 표시해요.
show-preview-button boolean true 미리보기 버튼을 표시해요(list-type이 image-card일 때). 이 이벤트에는 on-preview 콜백을 사용해요.
show-remove-button boolean true 제거 버튼을 표시해요(업로드가 완료된 뒤). 이 이벤트에는 on-remove 콜백을 사용해요.
show-retry-button boolean true 재시도 버튼을 표시해요(실패한 업로드).
show-trigger boolean true 업로드 트리거를 표시해요. 2.21.5
trigger-class string undefined 트리거 영역의 클래스예요. 2.36.0
trigger-style Object | string undefined 트리거 영역의 스타일이에요. 2.29.1
with-credentials boolean false 요청과 함께 보낼 자격 증명(예: 쿠키)이에요.
on-change (options: { file: UploadFileInfo, fileList: Array<UploadFileInfo>, event?: Event }) => void undefined 업로드 파일 상태가 바뀔 때의 콜백이에요.
on-error (options: { file: UploadFileInfo, event?: ProgressEvent }) => UploadFileInfo | void undefined 업로드가 실패했을 때의 콜백이에요. 2.24.0
on-finish (options: { file: UploadFileInfo, event?: Event }) => UploadFileInfo | undefined ({ file }) => file 업로드가 끝났을 때의 콜백이에요. 업로드된 UploadFileInfo를 가로채고 심지어 수정할 수도 있어요. 참고: file은 다음 이벤트 루프에서 null이 돼요.
on-before-upload (options: { file: UploadFileInfo, fileList: Array<UploadFileInfo> }) => (Promise<boolean | void> | boolean | void) true 업로드가 시작되기 직전의 콜백이에요. false를 반환하거나, false로 이행하는 프로미스, 또는 거부되는 프로미스를 반환하면 업로드가 취소돼요.
on-download (file: FileInfo) => Promise<boolean> | boolean | any undefined 다운로드 버튼을 클릭했을 때의 콜백이에요. false를 반환하거나, false로 이행하는 프로미스, 또는 거부되는 프로미스를 반환하면 다운로드가 취소돼요.
on-preview (file: FileInfo, detail: { event: MouseEvent }) => void undefined 파일 링크나 미리보기 버튼을 클릭했을 때의 콜백이에요. preventDefault를 쓰면 기본 앵커 링크 열기 동작을 막을 수 있어요. detail.event 2.39.0
on-remove (options: { file: UploadFileInfo, fileList: Array<UploadFileInfo>, index: number }) => Promise<boolean> | boolean | any () => true 파일 제거 콜백이에요. false를 반환하거나, false로 이행하는 프로미스, 또는 거부되는 프로미스를 반환하면 이 제거가 취소돼요. index 2.38.2
on-retry (options: { file: UploadFileInfo }) => (Promise<boolean | void> | boolean | void) undefined 재시도를 클릭했을 때의 콜백 함수예요. false를 반환하거나, false로 이행하는 프로미스, 또는 거부되는 프로미스를 반환하면 재시도가 취소돼요. 2.40.0
on-update:file-list (fileList: UploadFileInfo[]) => void undefined file-list가 바뀔 때마다 트리거되는 콜백 함수예요.

UploadFileInfo Type

속성 타입 설명 버전
id string | number 파일의 고유 ID예요. 이 값을 바꾸지 마세요. 관련 데이터를 저장해야 하면 컴포넌트 밖에서 Map 객체를 사용할 수 있어요.
name string 파일 이름이에요.
status 'pending' | 'uploading' | 'error' | 'finished' | 'removed' 파일의 상태예요.
batchId? string | null 업로드된 파일의 배치 ID예요. 같은 업로드 배치에서 선택된 파일들은 같은 batchId를 가져요. 2.28.3
file? File | null 브라우저에 저장된 파일 객체예요.
fullPath? string | null 업로드된 파일의 상대 경로예요. 2.28.3
percentage? number 파일이 업로드되는 진행률이에요.
thumbnailUrl? string | null 썸네일 URL이에요.
type? string | null MIME 타입이에요.
url? string | null 파일 URL이에요.

UploadTrigger Props

이름 타입 기본값 설명
abstract boolean false abstract 모드를 사용할지 여부예요.

UploadCustomRequestOptions Type

interface UploadCustomRequestOptions {
  file: FileInfo
  action?: string
  data?:
    | Record<string, string>
    | (({ file }: { file: FileInfo }) => Record<string, string>)
  withCredentials?: boolean
  headers?:
    | Record<string, string>
    | (({ file }: { file: FileInfo }) => Record<string, string>)
  onProgress: (e: { percent: number }) => void
  onFinish: () => void
  onError: () => void
}

Upload Methods

이름 타입 설명 버전
clear () => void 현재 업로드 목록을 지워요. 2.24.2
openOpenFileDialog () => void 파일 대화상자 창을 열어요.
submit (options?: { fileId?: string, retry?: boolean }) pending 상태의 모든 파일을 제출해요. retry: 2.44.0

Upload Slots

이름 파라미터 설명
default () upload의 콘텐츠예요.

UploadDragger Slots

이름 파라미터 설명
default () upload dragger의 플레이스홀더예요. 예시는 Drag to Upload Demo를 참고하세요.

UploadTrigger Slots

이름 파라미터 설명
default (options: { handleClick: () => void, handleDragOver: (e: DragEvent) => void, handleDragEnter: (e: DragEvent) => void, handleDragLeave: (e: DragEvent) => void, handleDrop: (e: DragEvent) => void}) handleClick은 클릭 업로드 함수, handleDrop은 드래그 앤 드롭 업로드 함수, handleDragEnter, handleDragOver, handleDragLeave는 드래그 앤 드롭 이벤트 함수예요.

Others

  1. uploadDownload은 타입이 (url: string, name: string | undefined): void인 메서드예요. naive-ui 패키지에서 직접 import할 수 있어요.

더 알아보기 (Learn more)

업로드 흐름을 완전히 통제하고 싶으면 custom-request와 UploadCustomRequestOptions 타입을 먼저 확인하세요. 드래그 앤 드롭 화면을 꾸미고 싶을 때는 UploadDragger 슬롯과 drag 데모를 참고하면 좋아요.