跳到内容
SCRM DOC

JImageUpload 图片上传组件使用指南

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

JImageUpload 图片上传组件

专门用于图片上传的组件,支持预览、拖拽排序、数量限制等功能。

基本用法

<template>
  <JImageUpload
    v-model:value="imageUrl"
    biz-path="avatar"
    text="上传图片"
  />
</template>

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

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

Props 属性

属性类型默认值说明
valuestring | array-图片路径(逗号分隔)
listTypestring'picture-card'上传列表样式:picture-card/picture
textstring'上传'按钮文字
bizPathstring来自配置业务路径(上传目录)
disabledbooleanfalse是否禁用
fileMaxnumber1最大上传数量
multiplebooleanfalse是否支持多选
draggablebooleanfalse是否开启拖拽排序
isCompressstring'N'是否压缩:Y/N

Events 事件

事件名参数说明
changevalue: string值变化时触发
update:valuevalue: string更新值
beforeUploadfile: File上传前触发
options-change-选项变化时触发

单张图片

<template>
  <JImageUpload
    v-model:value="avatar"
    biz-path="avatar"
    :file-max="1"
    text="上传头像"
  />
</template>

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

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

多张图片

<template>
  <JImageUpload
    v-model:value="images"
    biz-path="product"
    :file-max="9"
    text="上传产品图"
  />
</template>

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

const images = ref('');
// 返回格式:/temp/img1.jpg,/temp/img2.jpg
</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',
        fileMax: 1,
        text: '上传头像',
      },
      rules: [{ required: true, message: '请上传头像' }],
    },
    {
      field: 'images',
      label: '产品图片',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'product',
        fileMax: 9,
        text: '上传产品图',
      },
    },
  ],
});
</script>

拖拽排序

<template>
  <JImageUpload
    v-model:value="images"
    biz-path="product"
    :file-max="9"
    :draggable="true"
    text="上传产品图(可拖拽排序)"
  />
</template>

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

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

列表样式切换

picture-card 样式(默认)

<JImageUpload
  v-model:value="imageUrl"
  biz-path="avatar"
  list-type="picture-card"
  :file-max="1"
/>

picture 样式

<JImageUpload
  v-model:value="imageUrl"
  biz-path="avatar"
  list-type="picture"
  :file-max="1"
/>

图片预览

点击已上传的图片会自动打开预览:

<template>
  <JImageUpload
    v-model:value="imageUrl"
    biz-path="temp"
    text="上传图片"
  />
</template>

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

const imageUrl = ref('/temp/sample.jpg');
</script>

上传前验证

<template>
  <JImageUpload
    v-model:value="imageUrl"
    biz-path="avatar"
    :file-max="1"
    text="上传头像"
    @before-upload="handleBeforeUpload"
  />
</template>

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

const imageUrl = ref('');

function handleBeforeUpload(file) {
  // 限制文件大小为 2MB
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('图片大小不能超过 2MB');
    return false;
  }

  // 限制图片类型
  const validTypes = ['image/jpeg', 'image/png', 'image/jpg'];
  if (!validTypes.includes(file.type)) {
    message.error('只能上传 JPG/PNG 图片');
    return false;
  }

  return true;
}
</script>

图片压缩

<template>
  <JImageUpload
    v-model:value="imageUrl"
    biz-path="avatar"
    :is-compress="'Y'"
    :file-max="1"
    text="上传头像(自动压缩)"
  />
</template>

业务路径

bizPath说明
avatar用户头像
product商品图片
bannerBanner 图片
temp临时文件(默认)

完整示例

<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',
        fileMax: 1,
        text: '上传头像',
      },
      rules: [{ required: true, message: '请上传头像' }],
    },
    {
      field: 'banner',
      label: 'Banner',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'banner',
        fileMax: 1,
        listType: 'picture',
        text: '上传Banner',
      },
    },
    {
      field: 'gallery',
      label: '产品图库',
      component: 'JImageUpload',
      componentProps: {
        bizPath: 'product',
        fileMax: 9,
        draggable: true,
        text: '上传产品图(可拖拽排序)',
      },
    },
  ],
  labelWidth: 100,
});
</script>

相关组件

  • [[09. BasicForm 表单]] - 表单容器组件
  • 15. JUpload 文件上传 - 通用文件上传
  • [[05. Modal 弹窗]] - 弹窗组件
页面信息›
已优化是

本页目录