Skip to content

CdsUploader

The uploader sends files to the server right from the interface: the user picks them in the system dialog or drags them onto the button, and the component shows the list of what was uploaded — with a status, progress, downloading and removal. Every step of the file lifecycle comes with an event.

Storybook GitLab Figma
Test coverage: 97% — good coverage
API in specs
77 / 77 props, events, slots and exposed members with a test
Test cases
102 unit · 3 browser · 2 planned
Known bugs
18 pinned by an it.fails test
Code coverage
96.8% lines · 84.7% branches measured in v4.0.0-alpha.13
A11y (axe)
27 recommendations in 14 stories measured in v4.0.0-alpha.11

The uploader sends the files over HTTP itself. If the files only have to be picked and sent together with the form, take the CdsFileInput field, and for a drop zone with image previews — CdsFileUpload.

Anatomy ​

The first diagram is the frame: the label, the description, the upload button and the messages. The second one is an item of the file list during the upload: the status, the name as a link, the actions and the progress bar.

Variations ​

The upload button ​

By default the upload is started by the primary "Загрузить файл" button with the upload-icon icon. The button-props prop passes any CdsButton props to the button, and the default slot replaces it entirely — the ready props of the button arrive in the props slot argument.

Allowed file types ​

The accept prop limits the choice in the system dialog and filters out the unsuitable files on a drop — the rejected ones arrive in the reject event.

The file list ​

The list of the uploaded files lives in v-model — put the files received from the server into it and they will be shown right away. The show-upload-list prop controls the list: false hides it entirely, an object turns on the download and removal icons.

Number of files ​

The max-count prop limits the length of the list: with max-count="1" a new file replaces the previous one, with a larger limit the extra files are dropped.

Uploading directories ​

The directory prop turns on picking a whole folder — the files are collected from it recursively, on a drop as well.

States ​

disabled blocks the button and removes the removal icons from the list, readonly forbids changing the list but keeps the downloading.

Slots ​

When props are not enough, the content of any block is replaced by a slot.

Slot nameDescription
labelLabel content (replaces label)
descriptionDescription content (replaces description)
prependElement before the upload button
defaultYour own upload button; receives the ready props for CdsButton
appendElement after the upload button
messagesContent of the messages block
itemRenderYour own rendering of a list item; receives file, fileList and actions

Guidelines ​

Scenarios ​

Deferred sending ​

The before-upload hook returning false turns the automatic sending off: the files pile up in the list, and the upload is started by a separate button in the append slot — together with the submission of the whole form, for example.

A check before the upload ​

The same before-upload is the place for validation: return false and the file will not go to the server. A rejected file still gets into the list — remove it in the change handler and show the reason through error-messages.

Upload errors ​

A file rejected by the server stays in the list with the error status. The show-tooltip-with-error prop shows the server response in a tooltip next to the status, and the error event lets you collect the messages under the field — here they pile up in error-messages and are reset when the file is removed.

Uploading without a server ​

The custom-request prop replaces the built-in XHR transport with your own function: it receives the file and the onProgress, onSuccess, onError callbacks and returns an object with an abort method. Here the upload is simulated with a timer — files can be sent through an SDK or a WebSocket in the same way.

Events ​

The component reports every step: change — any change of the list, progress, success and error — the course of the upload, remove, drop, reject, download, preview — the actions of the user. Open the console to see them.

API ​

Props

NameTypeDefaultDescription
acceptstring | string[] | undefined''Allowed file types
actionNUploader.TAction | undefined''URL the files are uploaded to
activeColorstring | undefined—Color of the component in its active state
appendIconIconValue | undefined—Icon in the append slot, after the content
baseColorstring | undefined—Color of the component in its inactive state
beforeUpload((file: NUploader.IUploadFile<any>, fileList: NUploader.IUploadFile<any>[]) => NUploader.TBeforeUploadFileType | Promise<void | NUploader.TBeforeUploadFileType>) | undefined—Lifecycle hook called before a file is uploaded
bgColorstring | undefined—Background color of the component
buttonPropsPartial<CdsButtonProps> | undefinednullProps for the CdsButton component
captureboolean | "user" | "environment" | undefined—Type of the media the file is captured from on mobile devices
centerAffixboolean | undefinedtrueVertically center the appendInner, prependInner, clearIcon and label elements.
colorstring | undefined—Component color
customRequest((options: NUploader.IUploadRequestOption<any>) => void) | undefined—Your own file upload request. Overrides the default xhr
dataRecord<string, unknown> | ((file: string | Blob | NUploader.IRcFile) => Record<string, unknown>) | {} | undefined{}Parameters of the file upload or a function returning them
defaultFileListNUploader.IUploadFile<any>[] | undefined—Default list of files
descriptionstring | undefined—Description of the component, shown under the label
direction"horizontal" | "vertical" | undefined'horizontal'Switches the component between horizontal and vertical layout.
directoryboolean | undefined—Upload directories
disabledboolean | null | undefinednullDisabled state
downloadIconIconValue | undefined'download'File download icon
errorboolean | undefined—Puts the component into the error state
errorMessagesstring | readonly string[] | null | undefined[]Puts the component into the error state and passes custom error messages. They are merged with any checks defined in the rules prop. This field does not trigger validation.
focusedboolean | undefined—Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior.
headersNUploader.TUploadRequestHeaders | undefined—Request headers
hideDetailsboolean | "auto" | undefined—Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages
hideMessagesboolean | undefined—Hide the messages under the input
hintstring | undefined—Hint text shown under the input
iconColorstring | boolean | undefined—Icon color. Accepts a boolean or a color string
idstring | undefined—Unique identifier of the component
isImageUrl((file: NUploader.IUploadFile<any>) => boolean) | undefined—Configuration of the thumbnails of the uploaded files (not supported yet)
itemRenderNUploader.TItemRender<any> | undefined—Render function for an item of the uploaded file list
labelstring | undefined—Label text of the component
listTypeNUploader.TUploadListType | undefined'text'Type of the uploaded file list ('text' only is supported)
maxCountnumber | undefined—Maximum number of uploaded files
maxErrorsstring | number | undefined1Maximum number of validation errors shown
maxWidthstring | number | undefined—Maximum component width
messagesstring | readonly string[] | undefined[]Messages shown under the input
methodNUploader.TUploadRequestMethod | undefined—HTTP method of the upload request
minWidthstring | number | undefined—Minimum component width
modelValueany—Value of the component in v-model. If the component supports the multiple prop, an empty array is used by default.
multipleboolean | undefinedtrueUploading several files at once
namestring | undefined—Name of the input
openFileDialogOnClickboolean | undefined—Whether to open the system dialog on a click on the uploader
persistentHintboolean | undefined—Always show the hint, not only on focus
prependIconIconValue | undefined—Icon in the prepend slot, before the content
readonlyboolean | null | undefinednullRead-only state
removeIconIconValue | undefined'delete'File removal icon
rulesreadonly ValidationRule[] | undefined[]Accepts an array of items of different types: functions, booleans and strings. Functions take the input value as an argument and must return either true / false, or a string with an error message. The input goes into the error state if a function returns false (or any value in the array is false) or is a string.
showTooltipWithErrorboolean | undefinedfalseWhether to show a tooltip with the error next to every file
showUploadListboolean | NUploader.TShowUploadList | undefinedtrueShow the list of the uploaded files
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
tagstring | JSXComponent<any> | undefined'span'Custom HTML tag or component to render
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme
uploadIconIconValue | undefined'upload'File upload icon
validateOnValidateOn | undefined—Changes the type of the event that triggers validation
validationValueany—Value used when applying the validation rules
widthstring | number | undefined—Component width
withCredentialsboolean | undefined—Send the request with cookies

Events

NameTypeDescription
change[info: NUploader.IUploadChangeParam<NUploader.IUploadFile<any>>]Fires when the field changes
download[file: NUploader.IUploadFile<any>]Fires when a file is downloaded
drop[event: DragEvent]Fires when a file is dropped
error[error: ProgressEvent<EventTarget> | NUploader.IUploadRequestError, response: any, file: NUploader.IUploadFile<any>]Fires when a file upload fails
preview[file: NUploader.IUploadFile<any>]Fires when a file preview is opened
progress[event: { percent: number; } | undefined, file: NUploader.IUploadFile<any>]Fires while a file is being uploaded
reject[fileList: NUploader.IUploadFile<any>[]]Fires when a file upload is rejected
remove[file: NUploader.IUploadFile<any>]Fires when a file is removed
success[response: Record<string, unknown>, file: NUploader.IUploadFile<any>]Fires when a file has been uploaded successfully
update:modelValue[fileList: NUploader.IUploadFile<any>[]]Fires when the value changes (v-model)

Slots

NameTypeDescription
appendanyContent of the append slot after the upload button.
default{ props: Partial<CdsButtonProps>; }The upload button or activator. Receives the props for CdsButton.
descriptionanyDescription under the label.
itemRender{ originNode: VNode<RendererNode, RendererElement, { [key: string]: any; }>; file?: NUploader.IUploadFile<any> | undefined; fileList?: NUploader.IUploadFile<any>[] | undefined; actions: { download?: (() => void) | undefined; preview?: (() => void) | undefined; remove?: (() => void) | undefined; }; }Custom rendering of an item of the uploaded file list.
labelanyLabel of the upload field.
messagesanyHint and error messages.
prependanyContent of the prepend slot before the upload button.

Exposed

NameTypeDescription
fileListNUploader.IUploadFile<any>[]Current list of files together with their upload statuses.
uploadRefNUploader.IUploaderRef | undefinedRef to the inner upload element — gives access to choosing files programmatically and to aborting.