# 15. JUpload 文件上传
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/15.%20JUpload%20%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0

JUpload 文件上传组件使用指南


# JUpload 文件上传组件

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

## 基本用法

### 图片上传

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

### 文件上传

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

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

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

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `string \| array` | - | 文件路径（逗号分隔或数组） |
| text | `string` | `'上传'` | 按钮文字 |
| fileType | `string` | `'all'` | 文件类型：`image`/`all`/`file` |
| bizPath | `string` | `'temp'` | 业务路径（上传目录） |
| returnUrl | `boolean` | `true` | 是否只返回 URL |
| maxCount | `number` | `0` | 最大上传数量，0 为不限制 |
| buttonVisible | `boolean` | `true` | 是否显示上传按钮 |
| multiple | `boolean` | `true` | 是否支持多选 |
| mover | `boolean` | `true` | 是否显示左右移动按钮（图片模式） |
| download | `boolean` | `true` | 是否显示下载按钮 |
| removeConfirm | `boolean` | `false` | 删除时是否显示确认框 |
| disabled | `boolean` | `false` | 是否禁用 |
| beforeUpload | `function` | - | 上传前钩子 |
| replaceLastOne | `boolean` | `false` | 超出最大数量时是否替换最后一个 |

## Events 事件

| 事件名 | 参数 | 说明 |
|--------|------|------|
| change | `value: string \| array` | 值变化时触发 |
| update:value | `value: string \| array` | 更新值 |

## 文件类型

### image 图片模式

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

### all/file 文件模式

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

## 限制上传数量

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

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

const fileUrl = ref('');  // 单个文件
</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',
        maxCount: 1,
        text: '上传头像',
      },
    },
    {
      field: 'attachments',
      label: '附件',
      component: 'JUpload',
      componentProps: {
        bizPath: 'attachment',
        maxCount: 5,
        text: '上传附件',
      },
    },
  ],
});
</script>
```

## 返回值格式

### returnUrl=true（默认）

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

### returnUrl=false

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

## 上传前验证

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

## 删除确认

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

## 自定义样式

```vue
<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` | 合同文件 |

## 完整示例

```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',
        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`

## 相关组件

- [[09. BasicForm 表单]] - 表单容器组件
- [[19. JImageUpload 图片上传]] - 专门的图片上传组件
