data-upload
Enhance a file input with a drag-and-drop zone, file list display, and browse button. Works with native accept and multiple attributes.
Overview
The data-upload attribute enhances a native <input type="file"> with a drag-and-drop zone, visual file list, and styled prompt. No wrapper element needed — just add the attribute to the file input.
How It Works
Add data-upload to any <input type="file">. The init script:
- Creates a
.upload-zonewrapper withrole="presentation"around the input - Adds a
.upload-promptwith contextual text based onacceptandmultipleattributes - Creates a
.upload-file-list(<ul>witharia-label="Selected files") to display chosen files - Listens for drag events (
dragenter,dragleave,dragover,drop) on the zone - Sets
data-dragoveron the zone during active drag for visual feedback - Clicking anywhere in the zone triggers the hidden native file input
- On file selection, renders file names and sizes in the file list
The underlying input remains a real form control. It submits with the form, respects accept and multiple, and fires native change events.
Attributes
| Attribute | Type | Description |
|---|---|---|
data-upload |
boolean | Enables the drag-and-drop zone enhancement on the file input. |
data-upload-init |
boolean | Set automatically to prevent double-binding. Do not set manually. |
accept |
string | Native attribute. Filters allowed file types. Reflected in the prompt text. |
multiple |
boolean | Native attribute. Allows selecting multiple files. Changes prompt to plural text. |
DOM Structure
After initialization, the file input is wrapped in the following structure:
The native <input type="file"> is visually hidden but remains in the DOM for form submission and accessibility.
Accept Types
Use the native accept attribute to restrict file types. The prompt text updates to reflect allowed types. Dragged files that do not match the accept filter are rejected.
Multiple Files
Add the native multiple attribute to allow selecting more than one file. The file list displays all selected files with individual entries.
With Form Field
Wrap in <form-field> for validation feedback, helper text, and required indicators.
Drag States
During a drag operation, the data-dragover attribute is set on the .upload-zone element. Use it in CSS to provide visual feedback indicating the zone is an active drop target.
dragenter— setsdata-dragoverwhen a file enters the zonedragleave— removesdata-dragoverwhen the file leaves the zonedragover— prevents default to enable dropdrop— processes dropped files and removesdata-dragover
Events
The file input fires the native change event when files are selected via browse or drag-and-drop. Access the files from e.target.files.
You can also listen for drag events directly on the .upload-zone element:
Styling
The upload zone, prompt, and file list are fully customizable via CSS. All styles target the generated class names:
The dashed border and background color change during drag-over, providing clear visual feedback. The file list renders below the prompt with file names and formatted sizes.
Dynamic Elements
File inputs added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.
Accessibility
role="presentation"on the zone wrapper prevents it from being announced as an interactive elementaria-label="Selected files"on the file list gives screen readers context for the displayed file names- The native
<input type="file">remains in the DOM, preserving keyboard access and screen reader announcements - Clicking or pressing Enter/Space on the zone opens the native file picker dialog
- A visible
<label>is required for the file input acceptrestrictions are enforced on both browse and drop to prevent invalid file selection- Without JavaScript, the native file input renders normally — progressive enhancement