Skip to content

FileValidatorPlugin — 文件验证

优先级:0(最高)

验证文件大小、类型、是否为空,支持自定义验证函数。

基础用法

ts
import { FileValidatorPlugin } from "@file-ud.js/plugins/uploader";

uploader.use(new FileValidatorPlugin({
  maxSize: 10 * 1024 * 1024,    // 最大 10MB
  minSize: 1024,                 // 最小 1KB
  accept: ["image/*", ".pdf"],   // 允许的文件类型
  allowEmpty: false,             // 禁止空文件
}));

配置参数

参数类型默认值说明
maxSizenumberInfinity最大文件大小(字节)
minSizenumber0最小文件大小(字节)
acceptstring[]["*"]允许的文件类型,支持 MIME 和扩展名
allowEmptybooleanfalse是否允许空文件

使用场景

限制文件大小

ts
uploader.use(new FileValidatorPlugin({
  maxSize: 50 * 1024 * 1024,  // 最大 50MB
  minSize: 1024,               // 最小 1KB
}));

限制文件类型

ts
// 只允许图片和 PDF
uploader.use(new FileValidatorPlugin({
  accept: ["image/*", ".pdf"],
}));

// 只允许特定图片格式
uploader.use(new FileValidatorPlugin({
  accept: ["image/jpeg", "image/png", "image/webp"],
}));

禁止空文件

ts
uploader.use(new FileValidatorPlugin({
  allowEmpty: false,
}));

基于 MIT 协议发布