跳到内容
SCRM DOC

JUpload 文件上传组件使用指南

前端组件FormJUpload上传创建于2025-03-22更新于 2026-04-22

JUpload 文件上传组件

基于 Ant Design Vue Upload 封装的文件上传组件,支持图片、普通文件上传,限制数量、大小等。

基本用法

图片上传

<template>
  <JUpload
    v-model:value="imageUrl"
    file-type="image"
    biz-path="user"
    :max-count="1"
    text="上传头像"
  />
</template>

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

const imageUrl = ref('');
</script>

文件上传

<template>
  <JUpload
    v-model:value="fileUrls"
    biz-path="documents"
    text="上传文件"
  />
</template>

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

const fileUrls = ref('');
</script>

Props 属性

属性类型默认值说明
valuestring | array-文件路径(逗号分隔或数组)
textstring'上传'按钮文字
fileTypestring'all'文件类型:image/all/file
bizPathstring'temp'业务路径(上传目录)
returnUrlbooleantrue是否只返回 URL
maxCountnumber0最大上传数量,0 为不限制
buttonVisiblebooleantrue是否显示上传按钮
multiplebooleantrue是否支持多选
moverbooleantrue是否显示左右移动按钮(图片模式)
downloadbooleantrue是否显示下载按钮
removeConfirmbooleanfalse删除时是否显示确认框
disabledbooleanfalse是否禁用
beforeUploadfunction-上传前钩子
replaceLastOnebooleanfalse超出最大数量时是否替换最后一个

Events 事件

事件名参数说明
changevalue: string | array值变化时触发
update:valuevalue: string | array更新值

文件类型

image 图片模式

<JUpload
  v-model:value="images"
  file-type="image"
  biz-path="product"
  :max-count="5"
/>
<!-- 图片模式显示为 picture-card 样式 -->

all/file 文件模式

<JUpload
  v-model:value="files"
  file-type="file"
  biz-path="documents"
  text="上传文档"
/>
<!-- 文件模式显示为按钮列表样式 -->

限制上传数量

<template>
  <JUpload
    v-model:value="fileUrl"
    :max-count="1"
    text="上传单个文件"
  />
</template>

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

const fileUrl = ref('');  // 单个文件
</script>

在 BasicForm 中使用

<template>
  <BasicForm @register="registerForm" />
</template>

<script setup lang="ts">
import { useForm } from '@/components/Form';

const [registerForm] = useForm({
  schemas: [
    {
      field: 'avatar',
      label: '头像',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'avatar',
        maxCount: 1,
        text: '上传头像',
      },
    },
    {
      field: 'attachments',
      label: '附件',
      component: 'JUpload',
      componentProps: {
        bizPath: 'attachment',
        maxCount: 5,
        text: '上传附件',
      },
    },
  ],
});
</script>

返回值格式

returnUrl=true(默认)

<JUpload v-model:value="fileUrls" :return-url="true" />
<!-- 返回:逗号分隔的文件路径 -->
<!-- 示例:/temp/file1.pdf,/temp/file2.pdf -->

returnUrl=false

<JUpload v-model:value="fileJson" :return-url="false" />
<!-- 返回:JSON 字符串,包含文件名、路径、大小 -->
<!-- 示例:[{"fileName":"file1.pdf","filePath":"/temp/file1.pdf","fileSize":1024}] -->

上传前验证

<template>
  <JUpload
    v-model:value="fileUrl"
    :before-upload="beforeUpload"
    text="上传文件"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { message } from 'ant-design-vue';

const fileUrl = ref('');

function beforeUpload(file) {
  // 限制文件大小为 10MB
  const isLt10M = file.size / 1024 / 1024 < 10;
  if (!isLt10M) {
    message.error('文件大小不能超过 10MB');
    return false;  // 阻止上传
  }

  // 限制文件类型
  const isPdf = file.type === 'application/pdf';
  if (!isPdf) {
    message.error('只能上传 PDF 文件');
    return false;
  }

  return true;  // 允许上传
}
</script>

删除确认

<JUpload
  v-model:value="imageUrl"
  :remove-confirm="true"
  text="上传图片"
/>
<!-- 删除时会弹出确认对话框 -->

自定义样式

<template>
  <JUpload
    v-model:value="imageUrl"
    file-type="image"
    :max-count="1"
    :button-visible="false"
    :mover="false"
    :download="false"
  />
</template>

业务路径 (bizPath)

bizPath 用于指定文件上传的服务器目录:

bizPath说明
temp临时文件(默认)
avatar用户头像
product商品图片
documents文档资料
contract合同文件

完整示例

<template>
  <BasicForm @register="registerForm" />
</template>

<script setup lang="ts">
import { useForm } from '@/components/Form';

const [registerForm] = useForm({
  schemas: [
    {
      field: 'avatar',
      label: '头像',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'avatar',
        maxCount: 1,
        fileType: 'image',
        text: '上传头像',
      },
      rules: [{ required: true, message: '请上传头像' }],
    },
    {
      field: 'images',
      label: '产品图片',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'product',
        maxCount: 9,
        text: '上传产品图',
      },
    },
    {
      field: 'attachments',
      label: '附件',
      component: 'JUpload',
      componentProps: {
        bizPath: 'attachment',
        maxCount: 5,
        text: '上传附件',
        removeConfirm: true,
        beforeUpload: handleBeforeUpload,
      },
    },
  ],
  labelWidth: 100,
});

function handleBeforeUpload(file) {
  const isLt5M = file.size / 1024 / 1024 < 5;
  if (!isLt5M) {
    console.error('文件大小不能超过 5MB');
    return false;
  }
  return true;
}
</script>

相关文件

  • 组件路径:@/components/Form/src/jeecg/components/JUpload/JUpload.vue
  • 上传 API:@/api/common/api

相关组件

页面信息›
已优化是

本页目录