Skip to content

FaUploadImages 多图上传

多图上传,默认启用多文件模式,支持数量限制、预览列表、删除和 string[] | null 模型;清空后的模型值为 []。在手动上传模式下,选择文件时会立即校验文件类型和大小。拖拽与非拖拽使用一致的新增图片卡片布局;图片卡片尺寸和边框颜色沿用 Element Plus 默认值,调用方可通过对应 CSS Variables 自定义。

上传后的 URL 数组通过 v-model 获取,onChange(uploadFile, uploadFiles) 保持 Element Plus 原生文件状态回调。

多图上传、数量限制、预览与删除

查看代码
Vue
<template>
	<FaUploadImages v-model="images" :upload-api="uploadApi" :limit="5" />
</template>

<script setup lang="ts">
import { ref } from "vue";

const createImage = (text: string, color: string): string =>
	`data:image/svg+xml;charset=UTF-8,${encodeURIComponent(
		`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 200"><rect width="320" height="200" rx="18" fill="${color}"/><text x="160" y="115" text-anchor="middle" font-size="38" fill="white">${text}</text></svg>`
	)}`;
const images = ref<string[] | null>([createImage("图 1", "#67c23a"), createImage("图 2", "#e6a23c")]);
const uploadApi = (formData: FormData): Promise<string> => {
	const file = formData.get("file");
	if (!(file instanceof File)) return Promise.reject(new TypeError("未找到上传图片"));
	try {
		return Promise.resolve(URL.createObjectURL(file));
	} catch (error) {
		return Promise.reject(error);
	}
};
</script>

FaUploadImages 完整 API

Props 属性 (31)

Fast 与 Element Plus 2.14.6 的属性已合并展示;Fast 修改项优先排列,未透传的原生属性保留用于兼容性核对。

属性来源说明类型默认值
methodFast 修改上传 HTTP 方法。StringFastpostELpost
multipleFast 修改是否允许多选。BooleanFasttrueELfalse
acceptFast 修改允许选择的文件类型,格式与原生 input accept 属性一致。StringFast运行时生成EL""
fileListFast 修改上传文件列表的双向绑定值。ArrayFast[]EL[]
listTypeFast 修改上传文件列表样式。StringFastpicture-cardELtext
httpRequestFast 修改自定义上传请求实现,优先级最高。FunctionFastEL
limitFast 修改允许上传或选择的最大数量。NumberFast9EL
beforeUploadFast 修改上传前的校验回调。FunctionFastEL
modelValueFast 新增v-model 绑定值。Array
maxSizeFast 新增文件大小上限,单位 KB。String / Number2048
uploadApiFast 新增Fast 上传函数。Function
uploadUrlFast 新增Fast 内置上传请求地址。String
actionEL 原生上传请求地址。String#
headersEL 原生上传请求头。Object
dataEL 原生组件数据。Object / Function / Promise
nameEL 原生图标名称、CSS 类名或外部 SVG 地址。Stringfile
dragEL 原生whether to activate drag and drop mode.Booleanfalse
withCredentialsEL 原生whether cookies are sent.Booleanfalse
showFileListEL 原生是否显示文件列表。Booleantrue
autoUploadEL 原生选择文件后是否立即上传。Booleantrue
disabledEL 原生是否禁用组件或当前选项。Booleanfalse
directoryEL 原生whether to support uploading directory. After enabling it, only folders can be selected, and after selecting a folder, the files within the folder will be flattened.Booleanfalse
beforeRemoveEL 原生移除上传文件前的回调。Function
onRemoveEL 原生hook function when files are removed.Function
onChangeEL 原生hook function when select file or upload file success or upload file fail.Function
onPreviewEL 原生hook function when clicking the uploaded files.Function
onSuccessEL 原生hook function when uploaded successfully.Function
onProgressEL 原生hook function when some progress occurs.Function
onErrorEL 原生hook function when some errors occurs.Function
onExceedEL 原生hook function when limit is exceeded.Function
crossoriginEL 原生native attribute crossorigin.String
Events 事件(2)
名称来源说明参数 / 类型
update:modelValueFast 新增更新 v-model。
update:fileListFast 新增更新上传文件列表。
Slots 插槽(4)
名称来源说明参数 / 类型
defaultEL 原生组件默认内容插槽。
triggerEL 原生 · 未透传content which triggers file dialog.
tipEL 原生 · 未透传content of tips.
fileEL 原生 · 未透传content of thumbnail template.{ file: UploadFile, index: number }
Expose 暴露(9)
名称来源说明参数 / 类型
abortEL 原生中止指定文件或全部文件的上传请求。
submitEL 原生手动提交处于 ready 状态的上传文件。
clearFilesEL 原生清空上传文件列表,可指定文件状态。
handleStartEL 原生将原始文件加入上传队列。
handleRemoveEL 原生从上传列表移除指定文件。
loadingFast 新增Fast 业务加载状态。
fileListFast 新增当前上传文件列表。
previewFast 新增打开指定上传图片的预览。
previewListFast 新增当前可预览图片地址列表。

FaUploadImages 实例方法

abort 取消

取消上传请求

签名

ts
abort(file?: UploadFile): void;

示例

vue
<template>
	<FaUploadImages ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaUploadImages } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaUploadImages>>();
componentRef.value?.abort(undefined);
</script>

输入

输入值输入值类型必填/默认值输入值说明
fileUploadFile | undefined需要取消或移除的上传文件;省略时取消全部请求。

返回

返回值返回值类型返回值说明
resultvoid没有返回值。

submit 上传

手动上传文件列表

签名

ts
submit(): void;

示例

vue
<template>
	<FaUploadImages ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaUploadImages } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaUploadImages>>();
componentRef.value?.submit();
</script>

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultvoid没有返回值。

clearFiles 清空文件

清空已上传的文件列表(该方法不支持在 before-upload 中调用)

签名

ts
clearFiles(states?: import("element-plus").UploadStatus[]): void;

示例

vue
<template>
	<FaUploadImages ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaUploadImages } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaUploadImages>>();
componentRef.value?.clearFiles(["success"]);
</script>

输入

输入值输入值类型必填/默认值输入值说明
statesUploadStatus[] | undefined需要清除的上传状态集合。

返回

返回值返回值类型返回值说明
resultvoid没有返回值。

handleStart 加入文件

手动选择文件

签名

ts
handleStart(rawFile: import("element-plus").UploadRawFile): void;

示例

vue
<template>
	<FaUploadImages ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaUploadImages } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaUploadImages>>();
const rawFile = Object.assign(new File(["Fast"], "fast.txt", { type: "text/plain" }), { uid: Date.now() });
componentRef.value?.handleStart(rawFile);
</script>

输入

输入值输入值类型必填/默认值输入值说明
rawFileUploadRawFile浏览器选择的原始文件,并带有 Element Plus 上传 uid。

返回

返回值返回值类型返回值说明
resultvoid没有返回值。

handleRemove 移除文件

手动移除文件。file 和 rawFile 已被合并。

签名

ts
handleRemove(file: UploadFile | import("element-plus").UploadRawFile): void;

示例

vue
<template>
	<FaUploadImages ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaUploadImages } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaUploadImages>>();
const rawFile = Object.assign(new File(["Fast"], "fast.txt", { type: "text/plain" }), { uid: Date.now() });
componentRef.value?.handleRemove(rawFile);
</script>

输入

输入值输入值类型必填/默认值输入值说明
fileUploadRawFile | UploadFile需要取消或移除的上传文件;省略时取消全部请求。

返回

返回值返回值类型返回值说明
resultvoid没有返回值。