|
|
@@ -346,6 +346,54 @@ const handleBeforeUpload = (rawFile) => {
|
|
|
return false
|
|
|
}
|
|
|
}
|
|
|
+ // 文件类型校验
|
|
|
+ if (props.accept) {
|
|
|
+ // 解析accept字符串,支持MIME类型和文件扩展名
|
|
|
+ const acceptTypes = props.accept.split(',').map(type => type.trim())
|
|
|
+ let isAcceptable = false
|
|
|
+
|
|
|
+ // 获取文件的MIME类型和扩展名
|
|
|
+ const fileType = rawFile.type
|
|
|
+ const fileName = rawFile.name
|
|
|
+ const fileExt = fileName.substring(fileName.lastIndexOf('.')).toLowerCase()
|
|
|
+
|
|
|
+ // 检查是否符合任何一个accept类型
|
|
|
+ for (const acceptType of acceptTypes) {
|
|
|
+ if (acceptType === '') continue
|
|
|
+
|
|
|
+ // 检查MIME类型(如image/*、application/pdf)
|
|
|
+ if (acceptType.includes('/')) {
|
|
|
+ if (acceptType.endsWith('*')) {
|
|
|
+ // 通配符类型,如image/*
|
|
|
+ const mimePrefix = acceptType.substring(0, acceptType.indexOf('*'))
|
|
|
+ if (fileType.startsWith(mimePrefix)) {
|
|
|
+ isAcceptable = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 具体MIME类型,如application/pdf
|
|
|
+ if (fileType === acceptType) {
|
|
|
+ isAcceptable = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (acceptType.startsWith('.')) {
|
|
|
+ // 检查文件扩展名(如.jpg、.png)
|
|
|
+ if (fileExt === acceptType.toLowerCase()) {
|
|
|
+ isAcceptable = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isAcceptable) {
|
|
|
+ DGTMessage.error(`${t('common.fileTypeNotAllow')} ${props.accept}`)
|
|
|
+ emit('error', {
|
|
|
+ message: `${t('common.fileTypeNotAllow')} ${props.accept}`
|
|
|
+ })
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// 调用外部before-upload钩子
|
|
|
return emit('before-upload', rawFile) !== false
|