# 19. JImageUpload 图片上传
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/03.%20%E7%BB%84%E4%BB%B6%E5%BA%93%E6%96%87%E6%A1%A3/06.%20%E4%B8%8A%E4%BC%A0%E7%BB%84%E4%BB%B6/19.%20JImageUpload%20%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0

JImageUpload 图片上传组件使用指南


# JImageUpload 图片上传组件

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

## 基本用法

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

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

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

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `string \| array` | - | 图片路径（逗号分隔） |
| listType | `string` | `'picture-card'` | 上传列表样式：`picture-card`/`picture` |
| text | `string` | `'上传'` | 按钮文字 |
| bizPath | `string` | 来自配置 | 业务路径（上传目录） |
| disabled | `boolean` | `false` | 是否禁用 |
| fileMax | `number` | `1` | 最大上传数量 |
| multiple | `boolean` | `false` | 是否支持多选 |
| draggable | `boolean` | `false` | 是否开启拖拽排序 |
| isCompress | `string` | `'N'` | 是否压缩：`Y`/`N` |

## Events 事件

| 事件名 | 参数 | 说明 |
|--------|------|------|
| change | `value: string` | 值变化时触发 |
| update:value | `value: string` | 更新值 |
| beforeUpload | `file: File` | 上传前触发 |
| options-change | - | 选项变化时触发 |

## 单张图片

```vue
<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>
```

## 多张图片

```vue
<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 中使用

```vue
<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>
```

## 拖拽排序

```vue
<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 样式（默认）

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

### picture 样式

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

## 图片预览

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

```vue
<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>
```

## 上传前验证

```vue
<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>
```

## 图片压缩

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

## 业务路径

| bizPath | 说明 |
|---------|------|
| `avatar` | 用户头像 |
| `product` | 商品图片 |
| `banner` | Banner 图片 |
| `temp` | 临时文件（默认） |

## 完整示例

```vue
<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 弹窗]] - 弹窗组件
