Components/FileUploader

FileUploader

A controlled file picker with drag and drop and a file list.

VerifiedSince 0.3.4

Usage

Basic

accept limits the types and max the total count.

Loading demo...

Upload After Selection

add carries only the newly added files; change and update:modelValue carry the full list.

<script setup lang="ts">
import type { FileUploaderFile } from '@talex-touch/tuffex/file-uploader'
import { ref } from 'vue'

const files = ref<FileUploaderFile[]>([])

async function uploadAdded(added: FileUploaderFile[]) {
  await Promise.all(added.map(item => uploadFile(item.file)))
}
</script>

<template>
  <TxFileUploader v-model="files" accept="image/*" :max="3" @add="uploadAdded" />
</template>

Imperative Picker

pick() opens the native picker, unless disabled.

<script setup lang="ts">
const uploader = ref<{ pick: () => void }>()
</script>

<template>
  <TxFileUploader ref="uploader" v-model="files" />
  <TxButton @click="uploader?.pick()">Browse</TxButton>
</template>

Best Practices

  • Upload FileUploaderFile.file as the payload; the other fields only drive the UI.
  • Revalidate count, size, MIME, and content on the server; accept and max are UI constraints only.
  • Run upload side effects on add; listen to change when you need the full list.
  • Generated ids are valid only for the current UI session; don't persist them.
  • Set allowDrop=false in compact forms where an accidental drop is costly.

API Reference

Props

PropTypeDefaultDescription
modelValueFileUploaderFile[][]The selected files (controlled).
multiplebooleantruePassed to the native input.
acceptstring'*/*'Accepted file types; passed to the native input and applied the same way to dropped files.
disabledbooleanfalseBlocks browsing, removing, and dropping.
maxnumber10Maximum total number of selected files.
showSizebooleantrueShows file sizes in the list.
allowDropbooleantrueLets files be added by dropping.
buttonTextstring'Choose files'Browse button text.
dropTextstring'Drop files here'Primary drop-zone text.
hintTextstring'or click to browse'Drop-zone hint text.

Events

EventPayloadDescription
update:modelValueFileUploaderFile[]The full list after an add or remove.
changeFileUploaderFile[]Fires together with update:modelValue.
addFileUploaderFile[]The files accepted in this addition.
remove{ id: string, value: FileUploaderFile[] }The removed id and the remaining list.

Exposed Methods

NameTypeDescription
pick() => voidOpens the native file picker unless disabled.

Types

FileUploaderFile, one entry of the list:

FieldTypeDescription
idstringGenerated unique id, used as the list key and remove payload.
namestringOriginal file name.
sizenumberFile size in bytes.
typestringMIME type.
fileFileThe browser File object.

Overview

  • The drop zone is a native <button type="button"> that calls pick(); a hidden <input type="file"> takes multiple, accept, and disabled.
  • The native input is cleared after each pick, so the same file can be picked again.
  • With multiple, additions are capped at the remaining capacity max - modelValue.length, and nothing is emitted at zero; a single uploader replaces its file.
  • An addition emits add, update:modelValue, then change; a removal emits remove, update:modelValue, then change.
  • Drops are ignored when allowDrop=false or disabled; the root carries is-dragging during a drag.
  • With showSize, sizes render as B, KB, or MB.

Technologies

  • Source: packages/tuffex/packages/components/src/file-uploader/.
查看源码
packages/tuffex/packages/components/src/file-uploader/index.ts