-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathLocalFileUpload.vue
More file actions
156 lines (154 loc) · 5.15 KB
/
LocalFileUpload.vue
File metadata and controls
156 lines (154 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
<div v-loading="loading" class="w-full">
<el-upload
ref="UploadRef"
:webkitdirectory="false"
class="w-full"
drag
multiple
v-bind:file-list="modelValue"
action="#"
:auto-upload="false"
:show-file-list="false"
:accept="accept"
:limit="file_count_limit"
:on-exceed="onExceed"
:on-change="fileHandleChange"
@click.prevent="handlePreview(false)"
>
<img src="@/assets/upload-icon.svg" alt="" />
<div class="el-upload__text">
<p>
{{ $t('views.document.upload.uploadMessage') }}
<em class="hover" @click.prevent="handlePreview(false)">
{{ $t('views.document.upload.selectFile') }}
</em>
<em class="hover ml-4" @click.prevent="handlePreview(true)">
{{ $t('views.document.upload.selectFiles') }}
</em>
</p>
<div class="upload__decoration">
<p>
{{ $t('views.document.tip.fileLimitCountTip1') }} {{ file_count_limit }}
{{ $t('views.document.tip.fileLimitCountTip2') }},
{{ $t('views.document.tip.fileLimitSizeTip1') }} {{ file_size_limit }} MB
</p>
<p>{{ $t('views.document.upload.formats') }}{{ formats }}</p>
</div>
</div>
</el-upload>
<el-row :gutter="8" v-if="modelValue?.length" class="mt-16">
<template v-for="(item, index) in modelValue" :key="index">
<el-col :span="12" class="mb-8">
<el-card shadow="never" style="--el-card-padding: 8px 12px; line-height: normal">
<div class="flex-between">
<div class="flex">
<img :src="getImgUrl(item && item?.name)" alt="" width="40" />
<div class="ml-8">
<p class="ellipsis-1" :title="item && item?.name">{{ item && item?.name }}</p>
<el-text type="info" size="small">{{
filesize(item && item?.size) || '0K'
}}</el-text>
</div>
</div>
<el-button text @click="deleteFile(index)">
<AppIcon iconName="app-delete"></AppIcon>
</el-button>
</div>
</el-card>
</el-col>
</template>
</el-row>
</div>
</template>
<script setup lang="ts">
import { computed, useAttrs, nextTick, inject, ref } from 'vue'
import type { FormField } from '@/components/dynamics-form/type'
import { MsgError } from '@/utils/message'
import type { UploadFiles } from 'element-plus'
import { filesize, getImgUrl, fileType } from '@/utils/common'
import { t } from '@/locales'
const upload = inject('upload') as any
const attrs = useAttrs() as any
const props = withDefaults(defineProps<{ modelValue?: any; formField: FormField }>(), {
modelValue: () => [],
})
const onExceed = () => {
MsgError(
t('views.document.tip.fileLimitCountTip1') +
file_count_limit.value +
t('views.document.tip.fileLimitCountTip2'),
)
}
const emit = defineEmits(['update:modelValue'])
const fileArray = ref<any>([])
const loading = ref<boolean>(false)
const UploadRef = ref()
// 上传on-change事件
const fileHandleChange = (file: any, fileList: UploadFiles) => {
//1、判断文件大小是否合法,文件限制不能大于100M
const isLimit = file?.size / 1024 / 1024 < file_size_limit.value
if (!isLimit) {
MsgError(t('views.document.tip.fileLimitSizeTip1') + file_size_limit.value + 'MB')
fileList.splice(-1, 1) //移除当前超出大小的文件
return false
}
if (!file_type_list.value.includes(fileType(file.name).toLocaleUpperCase())) {
if (file?.name !== '.DS_Store') {
MsgError(t('views.document.upload.errorMessage2'))
}
fileList.splice(-1, 1)
return false
}
if (file?.size === 0) {
MsgError(t('views.document.upload.errorMessage3'))
fileList.splice(-1, 1)
return false
}
upload(file.raw, loading).then((ok: any) => {
const split_path = ok.data.split('/')
const file_id = split_path[split_path.length - 1]
fileArray.value?.push({ name: file.name, file_id, size: file.size })
emit('update:modelValue', fileArray.value)
})
}
function deleteFile(index: number) {
props.modelValue.splice(index, 1)
}
const handlePreview = (bool: boolean) => {
let inputDom: any = null
nextTick(() => {
if (document.querySelector('.el-upload__input') != null) {
inputDom = document.querySelector('.el-upload__input')
inputDom.webkitdirectory = bool
}
})
}
const accept = computed(() => {
return (attrs.file_type_list || []).map((item: any) => '.' + item.toLowerCase()).join(',')
})
const file_type_list = computed(() => {
return attrs.file_type_list || []
})
const formats = computed(() => {
return (attrs.file_type_list || []).map((item: any) => item.toUpperCase()).join('、')
})
const file_size_limit = computed(() => {
return attrs.file_size_limit || 50
})
const file_count_limit = computed(() => {
return attrs.file_count_limit || 100
})
</script>
<style lang="scss" scoped>
.upload__decoration {
font-size: 12px;
line-height: 20px;
color: var(--el-text-color-secondary);
}
.el-upload__text {
.hover:hover {
color: var(--el-color-primary-light-5);
}
}
</style>