WlFileUpload

Drop zone and file-selection button. The component stores a File[] list; the application sends files to the server.
Components / Inputs v0.1.0

Change props to see the result: the example and code use the same settings. “Reset” restores the initial values.

Settings

accept

Type filter (comma-separated extensions/MIME types).

maxFiles

File count limit.

maxSize

Maximum file size in bytes.

Multiple files (otherwise a new file replaces the current one).

Disables file selection and drag-and-drop.

Create local image previews while files remain selected.

Props fileState, pt, models, handlers and slots can be edited in the SFC. See their descriptions in API.

Example

Local demo: add images, PDF or TXT files up to 1 MiB. Advance progress, show an error and retry. Files are not sent to a server.

Drop files here or

Files selected: 0

Focus: outside the example · pointer: outside the example

Explore the live example and copy its Vue SFC into your application. Settings on the left can be combined.

Code for your application

Vue SFC · current settings
<script setup lang="ts">
import { WlButton, WlFileUpload } from "legato-ui";
import { ref } from "vue";
const files = ref<File[]>([]);
interface FileState { progress: number; status: "pending" | "uploading" | "success" | "error"; message?: string }
const states = ref(new Map<File, FileState>());
function fileState(file: File): FileState {
  return states.value.get(file) ?? { progress: 0, status: "pending" };
}
function advance(): void {
  for (const file of files.value) {
    const previous = fileState(file);
    if (previous.status === "error") continue;
    const progress = Math.min(100, previous.progress + 25);
    states.value.set(file, { progress, status: progress === 100 ? "success" : "uploading" });
  }
}
function fail(): void {
  const file = files.value[0];
  if (file) states.value.set(file, { ...fileState(file), status: "error", message: "Local example error. Retry to start again." });
}
function retry(file: File): void {
  states.value.set(file, { progress: 0, status: "pending" });
}
function clear(): void {
  files.value = [];
  states.value.clear();
}
</script>

<template>
  <div class="wl-stack" data-space="md">
    <p class="wl-text-small">{{ "Local demo: add images, PDF or TXT files up to 1 MiB. Advance progress, show an error and retry. Files are not sent to a server." }}</p>
    <WlFileUpload
      v-model="files"
      accept="image/*,.pdf,.txt"
      :max-files="3"
      :max-size="1048576"
      preview
      :file-state="fileState"
      
    >
      <template #row="{ file, size, previewUrl, state, remove }">
        <img
          v-if="previewUrl"
          class="wl-upload__preview"
          :src="previewUrl"
          alt=""
        />
        <span class="wl-upload__name">{{ file.name }}</span>
        <span class="wl-upload__size">{{ size }}</span>
        <progress
          class="wl-upload__progress"
          :value="state?.progress ?? 0"
          max="100"
          :aria-label="'File progress:' + ' ' + file.name"
        ></progress>
        <span v-if="state?.message" class="wl-upload__message">{{ state.message }}</span>
        <WlButton
          v-if="state?.status === 'error'"
          size="sm"
          variant="ghost"
          @click="retry(file)"
        >{{ "Retry" }}</WlButton>
        <WlButton
          size="sm"
          variant="ghost"
          :aria-label="'Remove' + ' ' + file.name"
          @click="remove"
        >{{ "Delete" }}</WlButton>
      </template>
    </WlFileUpload>
    <div class="wl-inline" data-space="sm">
      <WlButton size="sm" :disabled="!files.length" @click="advance">{{ "Advance demo progress" }}</WlButton>
      <WlButton
        size="sm"
        :disabled="!files.length"
        variant="ghost"
        @click="fail"
      >{{ "Show error" }}</WlButton>
      <WlButton size="sm" variant="ghost" @click="clear">{{ "Clear lists and errors" }}</WlButton>
    </div>
    <p class="wl-text-small" role="status">{{ "Files selected:" }} {{ files.length }}</p>
  </div>
</template>

Limits and local list

File type/size/count limits, replacement and removal, local image previews, controlled per-file progress and a row slot with error/retry.

Documents: type, size and count

PDF or TXT, up to two files, each no larger than 1 MiB. New files extend the list; the button next to a file removes it.

Drop files here or

One file replaces the previous file

An image up to 512 KiB. With multiple=false, the next valid selection replaces the current one.

Drop files here or

Disabled dropzone and clearing

Drop files here or

Enter/Space on the dropzone opens the file picker. Cancelling preserves the list. The component accepts File[] locally; the application uploads files to the server.

Documents: 0. Images: 0.

Vue SFC · additional example
<script setup lang="ts">
import { WlButton, type WlFileReject, WlFileUpload } from "legato-ui";
import { ref } from "vue";
const documents = ref<File[]>([]);
const image = ref<File[]>([]);
const revision = ref(0);
const lastRejection = ref("");
interface FileState { progress: number; status: "pending" | "uploading" | "success" | "error"; message?: string }
const states = ref(new Map<File, FileState>());
function fileState(file: File): FileState {
  return states.value.get(file) ?? { progress: 0, status: "pending" };
}
function advance(): void {
  for (const file of image.value) {
    const previous = fileState(file);
    if (previous.status === "error") continue;
    const progress = Math.min(100, previous.progress + 25);
    states.value.set(file, { progress, status: progress === 100 ? "success" : "uploading" });
  }
}
function fail(): void {
  const file = image.value[0];
  if (file) states.value.set(file, { ...fileState(file), status: "error", message: "Local example error. Retry to start again." });
}
function retry(file: File): void {
  states.value.set(file, { progress: 0, status: "pending" });
}
const reasons: Record<WlFileReject["reason"], string> = { type: "unsupported type", size: "size limit exceeded", count: "file count exceeded" };
function reject(payload: WlFileReject) {
  lastRejection.value = `${payload.file.name}: ${reasons[payload.reason]}.`;
}
function clear() {
  documents.value = [];
  image.value = [];
  states.value.clear();
  lastRejection.value = "";
  revision.value++;
}
</script>

<template>
  <div class="wl-stack" data-space="xl" data-input-example="WlFileUpload">
    <section class="wl-stack" data-space="md" aria-labelledby="docs-upload-documents">
      <h3 id="docs-upload-documents" class="wl-text-title">{{ "Documents: type, size and count" }}</h3>
      <p class="wl-text-small">{{ "PDF or TXT, up to two files, each no larger than 1 MiB. New files extend the list; the button next to a file removes it." }}</p>
      <WlFileUpload
        :key="`documents-${revision}`"
        v-model="documents"
        accept=".pdf,.txt"
        :max-files="2"
        :max-size="1048576"
        @reject="reject"
      />
      <p v-if="lastRejection" class="wl-text-small docs-upload-message">{{ "Last reject event:" }} {{ lastRejection }}</p>
    </section>
    <section class="wl-stack" data-space="md" aria-labelledby="docs-upload-image">
      <h3 id="docs-upload-image" class="wl-text-title">{{ "One file replaces the previous file" }}</h3>
      <p class="wl-text-small">{{ "An image up to 512 KiB. With multiple=false, the next valid selection replaces the current one." }}</p>
      <WlFileUpload
        :key="`image-${revision}`"
        v-model="image"
        accept="image/*"
        :multiple="false"
        :max-size="524288"
        preview
        :file-state="fileState"
        @reject="reject"
      >
        <template #row="{ file, size, previewUrl, state, remove }">
          <img
            v-if="previewUrl"
            class="wl-upload__preview"
            :src="previewUrl"
            alt=""
          />
          <span class="wl-upload__name">{{ file.name }}</span>
          <span class="wl-upload__size">{{ size }}</span>
          <progress
            class="wl-upload__progress"
            :value="state?.progress ?? 0"
            max="100"
            :aria-label="'File progress:' + ' ' + file.name"
          ></progress>
          <span v-if="state?.message" class="wl-upload__message">{{ state.message }}</span>
          <WlButton
            v-if="state?.status === 'error'"
            size="sm"
            variant="ghost"
            @click="retry(file)"
          >{{ "Retry" }}</WlButton>
          <WlButton
            size="sm"
            variant="ghost"
            :aria-label="'Remove' + ' ' + file.name"
            @click="remove"
          >{{ "Delete" }}</WlButton>
        </template>
      </WlFileUpload>
      <div class="wl-inline" data-space="sm">
        <WlButton size="sm" :disabled="!image.length" @click="advance">{{ "Advance demo progress" }}</WlButton>
        <WlButton
          size="sm"
          :disabled="!image.length"
          variant="ghost"
          @click="fail"
        >{{ "Show error" }}</WlButton>
      </div>
    </section>
    <section class="wl-stack" data-space="md">
      <h3 class="wl-text-title">{{ "Disabled dropzone and clearing" }}</h3>
      <WlFileUpload :model-value="[]" disabled />
      <WlButton size="sm" @click="clear">{{ "Clear lists and errors" }}</WlButton>
      <p class="wl-text-small wl-text-muted">{{ "Enter/Space on the dropzone opens the file picker. Cancelling preserves the list. The component accepts File[] locally; the application uploads files to the server." }}</p>
      <p class="wl-text-small" role="status">{{ "Documents:" }} {{
        documents.length
      }}{{ ". Images:" }} {{ image.length }}.</p>
    </section>
  </div>
</template>

<style scoped>
.docs-upload-message { overflow-wrap: anywhere; }
</style>