Skip to content

上传 UploadFile

文件上传,总归是会用到的。

类型定义

ts
/**
 * 文件列表类型
 * @property name 文件名
 * @property url 文件路径
 */
interface FileInfo {
  /** 文件名 */
  name: string;
  /** 文件路径 */
  url: string;
}
ts
/**
 * 移除文件类型
 * @property idx 文件索引
 * @property name 文件名
 * @property url 文件路径
 */
interface RemoveFile {
  /** 文件名 */
  name: string;
  /** 文件路径 */
  url: string;
  /** 文件索引 */
  idx: number;
}

基本使用

vue
<template>
  <UploadFile
    :value="imgUrl"
    :limit="1"
    @upload="(val) => (imgUrl = val.url)"
    @remove="imgUrl = ''"
  />
</template>

<script setup lang="ts">
const imgUrl = ref<string>(""); // 图片地址,字符串类型
</script>
vue
<template>
  <UploadFile
    :value="imgUrls"
    @upload="(val) => imgUrls.push(val.url)"
    @remove="
      (val) => {
        if (imgUrls[idx] === val.url) {
          imgUrls.splice(idx, 1);
        }
      }
    "
  />
</template>

<script setup lang="ts">
const imgUrls = ref<string[]>([]); // 图片地址,数组类型
</script>

💡 提示

这仅是一个普通用法,更多用法请查看 案例

Props

名称类型必传默认值说明
valuestring[]
string
FileInfo[]
FileInfo
[]文件列表
dataObject{}提交表单需要附加的数据
nameStringfile上传文件的参数名
limitNumber10文件上传数量限制
max-file-sizeNumber10单个文件的最大允许大小
acceptStringimage/*允许上传的文件类型,参考 accept
typeNUpload[list-type]image-card文件列表的内建样式
multipleBooleantrue是否支持多选,在 limit1 时无效
dragBooleanfalse是否支持拖拽上传
drag-optionsDragOptionsDragOptions拖拽上传配置,仅在 dragtrue 时有效
其他参数Upload-PropsNUpload 组件参数

DragOptions

名称类型默认值说明
iconStringep:upload-filled图标
iconSizeNumber50图标大小
titleString点击此处 或 拖动文件到该区域进行上传标题

Slots

属性参数说明
default()默认插槽,触发器等

Events

名称类型说明
upload(val: FileInfo) => val文件上传成功回调
remove(val: RemoveFile) => val移除文件回调

贡献者

页面历史

基于 MIT 许可发布