File upload

A drop zone to pick files: drag, click, keyboard or paste. Checks type, size and count as files arrive, lists them as ray-attachment rows (thumbnails, remove) and submits them with its form.

Source on GitHub

Code

In a raylang app

In a std/ui window with the native bridge, native makes browsing open the system's file dialog, with the accept types as its filter. The files it gives are paths on the user's disk, not bytes: they list like any other file (name, size, type) and the program reads them.

<form id="import">
  <ray-file-upload name="photos" native multiple accept="image/*" label="Photos"></ray-file-upload>
</form>
upload.addEventListener("change", () => {
  window.ray.send("import:" + JSON.stringify(upload.paths));   // ["/Users/ada/Pictures/a.jpg", …]
});

The form submits those paths under name. Dropped and pasted files are still File objects (upload.files holds both; a chosen one is a HostFile with a path). Without the bridge, or in a browser, native changes nothing.

API

For AI agents: this API as Markdown, or every component in llms-full.txt.

<ray-file-upload> · RayFileUpload

A drag that can only be refused turns the zone red before the drop; refused files fire ray-reject and are explained under the zone. Without multiple a new file replaces the current one. Uploading is the app's: attachmentFor(file) gives the row whose progress/status/error to set.

PropertyAttributeTypeDefaultDescription
namename reflectsstringForm field name: every file is submitted under it.
labellabelstringLabel above the zone.
descriptiondescriptionstringHelp text under the label.
errorerrorstringError text (replaces the explanation of refused files).
requiredrequired reflectsbooleanThe form needs at least one file.
disableddisabled reflectsbooleanNo picking, dropping or removing.
acceptaccept reflectsstringAccepted types, as in <input type=file>: ".pdf,.png", "image/*", "application/json".
multiplemultiple reflectsbooleanSeveral files (else one, replaced by the next).
maxSizemax-sizestringLargest file: bytes or a size like "10 MB", "512 KB".
maxFilesmax-filesnumberWith multiple: the most files kept.
hinthintstringSmall text in the zone. Default: from accept, max-size and max-files ("PNG or PDF · up to 10 MB each · max. 5 files").
dropLabeldrop-labelstringDrag files here (text file-upload.drop)Title of the zone, before "or browse".
browseLabelbrowse-labelstringbrowse (text file-upload.browse)The link-styled word (the button text in compact).
activeLabelactive-labelstringDrop to add files (text file-upload.active)Title while files are dragged over it.
tapLabeltap-labelstringChoose files (text file-upload.tap)Title on touch screens (no hover), where dragging files is rare.
variantvariant reflectsdefault | compactdefaultdefault: a tall centered zone; compact: one row with a Browse button.
listlist reflectsrows | cards | nonerowsHow chosen files show: full-width rows, thumbnail cards, or not at all (render your own on change).
nativenative reflectsbooleanWith a native host (ray-elements/core/raylang.js in a std/ui window), browsing opens the system dialog and gives paths (see `paths`); drop and paste still give Files.
colorcolor reflectsstringAccent of hover, drag and the browse link (default: primary).
EventDescription
changeFiles were added or removed (read `files`, and `lastChange` for which).
ray-rejectSome files were refused (type, size or count).
ray-errorThe native dialog failed (with `native`).
SlotDescription
iconReplaces the upload icon.
PartDescription
dropzoneThe zone (a button).
iconThe icon tile.
hintThe small text in the zone.
filesThe list of chosen files.
labelThe label.
errorThe error text.

Texts

What it writes itself, in English; setStrings() translates them (Getting started: Languages).

KeyEnglish
file-upload.dropDrag files here
file-upload.browsebrowse
file-upload.activeDrop to add files
file-upload.tapChoose files
file-upload.imagesImages
file-upload.videosVideos
file-upload.audioAudio
file-upload.textText files
file-upload.types{list} or {last}
file-upload.too-large{name} is larger than {size}.
file-upload.only-oneOnly one file can be added.
file-upload.max{ one: "Up to {count} file can be added.", other: "Up to {count} files can be added." }
file-upload.fullNo more files can be added
file-upload.room{ one: "Only {count} more file", other: "Only {count} more files" }
file-upload.refusedFile type not allowed
file-upload.requiredSelect at least one file.
file-upload.hint-sizeup to {size}
file-upload.hint-size-eachup to {size} each
file-upload.hint-max{ one: "max. {count} file", other: "max. {count} files" }