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 name | Description |
|---|---|
label | Label content (replaces label) |
description | Description content (replaces description) |
prepend | Element before the upload button |
default | Your own upload button; receives the ready props for CdsButton |
append | Element after the upload button |
messages | Content of the messages block |
itemRender | Your 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
| Name | Type | Default | Description |
|---|---|---|---|
accept | string | string[] | undefined | '' | Allowed file types |
action | NUploader.TAction | undefined | '' | URL the files are uploaded to |
activeColor | string | undefined | — | Color of the component in its active state |
appendIcon | IconValue | undefined | — | Icon in the append slot, after the content |
baseColor | string | 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 |
bgColor | string | undefined | — | Background color of the component |
buttonProps | Partial<CdsButtonProps> | undefined | null | Props for the CdsButton component |
capture | boolean | "user" | "environment" | undefined | — | Type of the media the file is captured from on mobile devices |
centerAffix | boolean | undefined | true | Vertically center the appendInner, prependInner, clearIcon and label elements. |
color | string | undefined | — | Component color |
customRequest | ((options: NUploader.IUploadRequestOption<any>) => void) | undefined | — | Your own file upload request. Overrides the default xhr |
data | Record<string, unknown> | ((file: string | Blob | NUploader.IRcFile) => Record<string, unknown>) | {} | undefined | {} | Parameters of the file upload or a function returning them |
defaultFileList | NUploader.IUploadFile<any>[] | undefined | — | Default list of files |
description | string | undefined | — | Description of the component, shown under the label |
direction | "horizontal" | "vertical" | undefined | 'horizontal' | Switches the component between horizontal and vertical layout. |
directory | boolean | undefined | — | Upload directories |
disabled | boolean | null | undefined | null | Disabled state |
downloadIcon | IconValue | undefined | 'download' | File download icon |
error | boolean | undefined | — | Puts the component into the error state |
errorMessages | string | 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. |
focused | boolean | undefined | — | Controls the focus state of the component. When true, the component is considered focused and may apply the corresponding styles or behavior. |
headers | NUploader.TUploadRequestHeaders | undefined | — | Request headers |
hideDetails | boolean | "auto" | undefined | — | Hides the details block (messages and hints). With the 'auto' value the block is hidden when there are no messages |
hideMessages | boolean | undefined | — | Hide the messages under the input |
hint | string | undefined | — | Hint text shown under the input |
iconColor | string | boolean | undefined | — | Icon color. Accepts a boolean or a color string |
id | string | undefined | — | Unique identifier of the component |
isImageUrl | ((file: NUploader.IUploadFile<any>) => boolean) | undefined | — | Configuration of the thumbnails of the uploaded files (not supported yet) |
itemRender | NUploader.TItemRender<any> | undefined | — | Render function for an item of the uploaded file list |
label | string | undefined | — | Label text of the component |
listType | NUploader.TUploadListType | undefined | 'text' | Type of the uploaded file list ('text' only is supported) |
maxCount | number | undefined | — | Maximum number of uploaded files |
maxErrors | string | number | undefined | 1 | Maximum number of validation errors shown |
maxWidth | string | number | undefined | — | Maximum component width |
messages | string | readonly string[] | undefined | [] | Messages shown under the input |
method | NUploader.TUploadRequestMethod | undefined | — | HTTP method of the upload request |
minWidth | string | number | undefined | — | Minimum component width |
modelValue | any | — | Value of the component in v-model. If the component supports the multiple prop, an empty array is used by default. |
multiple | boolean | undefined | true | Uploading several files at once |
name | string | undefined | — | Name of the input |
openFileDialogOnClick | boolean | undefined | — | Whether to open the system dialog on a click on the uploader |
persistentHint | boolean | undefined | — | Always show the hint, not only on focus |
prependIcon | IconValue | undefined | — | Icon in the prepend slot, before the content |
readonly | boolean | null | undefined | null | Read-only state |
removeIcon | IconValue | undefined | 'delete' | File removal icon |
rules | readonly 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. |
showTooltipWithError | boolean | undefined | false | Whether to show a tooltip with the error next to every file |
showUploadList | boolean | NUploader.TShowUploadList | undefined | true | Show the list of the uploaded files |
size | number | "xs" | "sm" | "md" | "lg" | "xl" | undefined | 'md' | Size of the component |
tag | string | JSXComponent<any> | undefined | 'span' | Custom HTML tag or component to render |
textColor | string | undefined | — | Text color of the component |
theme | string | undefined | — | Component theme |
uploadIcon | IconValue | undefined | 'upload' | File upload icon |
validateOn | ValidateOn | undefined | — | Changes the type of the event that triggers validation |
validationValue | any | — | Value used when applying the validation rules |
width | string | number | undefined | — | Component width |
withCredentials | boolean | undefined | — | Send the request with cookies |
Events
| Name | Type | Description |
|---|---|---|
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
| Name | Type | Description |
|---|---|---|
append | any | Content of the append slot after the upload button. |
default | { props: Partial<CdsButtonProps>; } | The upload button or activator. Receives the props for CdsButton. |
description | any | Description 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. |
label | any | Label of the upload field. |
messages | any | Hint and error messages. |
prepend | any | Content of the prepend slot before the upload button. |
Exposed
| Name | Type | Description |
|---|---|---|
fileList | NUploader.IUploadFile<any>[] | Current list of files together with their upload statuses. |
uploadRef | NUploader.IUploaderRef | undefined | Ref to the inner upload element — gives access to choosing files programmatically and to aborting. |