File Upload
Two modes for collecting files: a drag-and-drop zone for prominent upload areas, or a compact browse button for inline forms
Built-in Features
Activates when .nds-file-upload is on the page. Dynamic elements added later are picked up automatically.
Files can be dragged onto the drop zone with visual feedback on hover. Toggled on and off with data-state="dropbox".
Validates file size, extension, and MIME type before upload. Rejected files appear in the list with an error message in Arabic or English.
File names are sanitized to strip path traversal sequences, null bytes, and control characters before display and upload.
Five status stages (ready, uploading, processing, complete, error) with progress tracking, retry for failures, and abort for in-progress uploads.
Full JavaScript API to add, remove, upload, retry, and abort files. Intercept uploads via the cancelable beforeUpload event to set custom headers.
Error and validation messages display in Arabic or English based on the page language setting.
Nine custom events cover the full upload lifecycle, letting you hook into file selection, progress updates, success, and error handling.
Usage Guidelines
Best Practices
- Use the drop zone mode (
data-state="dropbox") for dedicated upload areas where file selection is the primary action on the page - Use the browse button mode (no dropbox state) when file upload is one field among many in a form
- Use single file mode (
data-state="single") for profile photos, document replacements, or anywhere only one file is expected - Always set
data-max-file-sizeanddata-allowed-typesto give users immediate validation feedback rather than waiting for server rejection - Set
data-max-fileswhen the server has a file count limit. Excess files appear in the list with an error so users understand why they were rejected - Use the
nds:upload:beforeUploadevent to add authorization headers, CSRF tokens, or extra form fields. The component does not handle authentication. - Do not use this component for large file transfers (500MB+) that need chunked upload or resumable protocols. Build a custom solution with the events API as a starting point
- Server-side validation must duplicate all client-side checks. Client validation improves UX but cannot be trusted for security
- Combine
data-allowed-types(extension) withdata-allowed-mime-typesfor defense in depth: extensions can be spoofed, MIME types add a second check
Data Attributes
| Attribute | Description |
|---|---|
data-state="dropbox" | Enables the drag-and-drop zone UI with dashed border and upload icon |
data-state="single" | Single file mode: new selection replaces the current file |
data-upload-url | Server endpoint for XHR file uploads (POST) |
data-auto-upload="true" | Automatically upload files on selection instead of waiting for startUpload() |
data-max-file-size | Maximum file size in bytes. Default: 10485760 (10 MB) |
data-max-files | Maximum number of files allowed. Default: unlimited |
data-allowed-types | Comma-separated file extensions: jpg,png,pdf |
data-allowed-mime-types | Comma-separated MIME types, supports wildcards: image/*,application/pdf |
Events
| Event | Detail |
|---|---|
nds:upload:ready | { instance } |
nds:upload:selected | { files, allFiles, fileData } |
nds:upload:validationError | { errors } |
nds:upload:beforeUpload (cancelable) | { fileData, formData, xhr } |
nds:upload:progress | { fileData, progress } |
nds:upload:success | { fileData, response } |
nds:upload:error | { fileData, error, status } |
nds:upload:removed | { fileData, fileId } |
nds:upload:maxFilesReached | { maxFiles, currentCount } |
JavaScript API
The NDS.Upload API provides static methods to access instances and instance methods to manage files, trigger uploads, and control the component state.