Skip to content

Commit a77416f

Browse files
committed
feat(FaImageUpload): 增加文件夹上传和自定义上传请求功能
1 parent 2302186 commit a77416f

7 files changed

Lines changed: 259 additions & 289 deletions

File tree

‎apps/example/src/views/component_example/image_upload/_demo3.vue‎

100755100644
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
<script setup lang="ts">
22
const files = ref<string[]>([])
33
4+
function beforeUpload(file: File) {
5+
if (!file.type.startsWith('image/')) {
6+
faToast.error('请选择图片文件')
7+
return false
8+
}
9+
if (file.size > 200 * 1024) {
10+
faToast.error('图片大小不能超过 200KB')
11+
return false
12+
}
13+
return true
14+
}
15+
416
function handleSuccess() {
517
faToast.success('模拟上传成功')
618
}
@@ -11,11 +23,9 @@ function handleSuccess() {
1123
v-model="files"
1224
action="/fake/upload"
1325
:after-upload="(response) => `${response.data.url}?fake=${Math.random()}`"
26+
:before-upload="beforeUpload"
1427
:width="200"
1528
:height="130"
16-
:dimension="{ width: 400, height: 260 }"
17-
:ext="['png']"
18-
:size="200 * 1024"
1929
:max="0"
2030
@on-success="handleSuccess"
2131
>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script setup lang="ts">
2+
import type { UploadRequestOptions } from '@fantastic-admin/components'
3+
4+
const files = ref<string[]>([])
5+
6+
async function httpRequest({ file, onProgress }: UploadRequestOptions) {
7+
onProgress(30)
8+
await new Promise(resolve => setTimeout(resolve, 300))
9+
onProgress(100)
10+
11+
return {
12+
url: URL.createObjectURL(file),
13+
name: file.name,
14+
}
15+
}
16+
17+
function handleSuccess() {
18+
faToast.success('自定义上传完成')
19+
}
20+
</script>
21+
22+
<template>
23+
<FaImageUpload
24+
v-model="files"
25+
:http-request="httpRequest"
26+
:after-upload="response => response.url"
27+
:max="3"
28+
@on-success="handleSuccess"
29+
/>
30+
</template>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<script setup lang="ts">
2+
const files = ref<string[]>([])
3+
4+
function beforeUpload(file: File) {
5+
return file.type.startsWith('image/')
6+
}
7+
8+
async function httpRequest({ file, onProgress }: {
9+
file: File
10+
onProgress: (percent: number) => void
11+
}) {
12+
onProgress(50)
13+
await new Promise(resolve => setTimeout(resolve, 200))
14+
onProgress(100)
15+
16+
return {
17+
url: URL.createObjectURL(file),
18+
name: file.name,
19+
}
20+
}
21+
</script>
22+
23+
<template>
24+
<FaImageUpload
25+
v-model="files"
26+
directory
27+
:max="0"
28+
:http-request="httpRequest"
29+
:before-upload="beforeUpload"
30+
:after-upload="response => response.url"
31+
/>
32+
</template>

‎apps/example/src/views/component_example/image_upload/index.vue‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import Demo1 from './_demo1.vue'
33
import Demo2 from './_demo2.vue'
44
import Demo3 from './_demo3.vue'
5+
import Demo4 from './_demo4.vue'
6+
import Demo5 from './_demo5.vue'
57
</script>
68

79
<template>
@@ -13,8 +15,14 @@ import Demo3 from './_demo3.vue'
1315
<FaPageMain title="多图上传" main-class="p-4">
1416
<Demo2 />
1517
</FaPageMain>
16-
<FaPageMain title="自定义默认内容、容器大小、建议尺寸、限制类型、不限制数量" main-class="p-4">
18+
<FaPageMain title="上传前校验" main-class="p-4">
1719
<Demo3 />
1820
</FaPageMain>
21+
<FaPageMain title="自定义上传请求" main-class="p-4">
22+
<Demo4 />
23+
</FaPageMain>
24+
<FaPageMain title="文件夹上传" main-class="p-4">
25+
<Demo5 />
26+
</FaPageMain>
1927
</div>
2028
</template>

‎packages/components/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export { default as FaHoverCard } from './src/hover-card/index.vue'
1717
export { default as FaIcon } from './src/icon/index.vue'
1818
export * from './src/image-preview/index'
1919
export { default as FaImagePreview } from './src/image-preview/index.vue'
20+
export type { UploadRequestOptions } from './src/image-upload/index.vue'
2021
export { default as FaImageUpload } from './src/image-upload/index.vue'
2122
export { default as FaInputOTP } from './src/input-otp/index.vue'
2223
export { default as FaInput } from './src/input/index.vue'

0 commit comments

Comments
 (0)