@@ -116,6 +116,64 @@ export function toAnyString() {
116
116
return str
117
117
}
118
118
119
+ /**
120
+ * 根据支持的文件类型生成 accept 属性值
121
+ *
122
+ * @param supportedFileTypes 支持的文件类型数组,如 ['PDF', 'DOC', 'DOCX']
123
+ * @returns 用于文件上传组件 accept 属性的字符串
124
+ */
125
+ export const generateAcceptedFileTypes = ( supportedFileTypes : string [ ] ) : string => {
126
+ const allowedExtensions = supportedFileTypes . map ( ( ext ) => ext . toLowerCase ( ) )
127
+ const mimeTypes : string [ ] = [ ]
128
+
129
+ // 添加常见的 MIME 类型映射
130
+ if ( allowedExtensions . includes ( 'txt' ) ) {
131
+ mimeTypes . push ( 'text/plain' )
132
+ }
133
+ if ( allowedExtensions . includes ( 'pdf' ) ) {
134
+ mimeTypes . push ( 'application/pdf' )
135
+ }
136
+ if ( allowedExtensions . includes ( 'html' ) || allowedExtensions . includes ( 'htm' ) ) {
137
+ mimeTypes . push ( 'text/html' )
138
+ }
139
+ if ( allowedExtensions . includes ( 'csv' ) ) {
140
+ mimeTypes . push ( 'text/csv' )
141
+ }
142
+ if ( allowedExtensions . includes ( 'xlsx' ) || allowedExtensions . includes ( 'xls' ) ) {
143
+ mimeTypes . push ( 'application/vnd.ms-excel' )
144
+ mimeTypes . push ( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' )
145
+ }
146
+ if ( allowedExtensions . includes ( 'docx' ) || allowedExtensions . includes ( 'doc' ) ) {
147
+ mimeTypes . push ( 'application/msword' )
148
+ mimeTypes . push ( 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' )
149
+ }
150
+ if ( allowedExtensions . includes ( 'pptx' ) || allowedExtensions . includes ( 'ppt' ) ) {
151
+ mimeTypes . push ( 'application/vnd.ms-powerpoint' )
152
+ mimeTypes . push ( 'application/vnd.openxmlformats-officedocument.presentationml.presentation' )
153
+ }
154
+ if ( allowedExtensions . includes ( 'xml' ) ) {
155
+ mimeTypes . push ( 'application/xml' )
156
+ mimeTypes . push ( 'text/xml' )
157
+ }
158
+ if ( allowedExtensions . includes ( 'md' ) || allowedExtensions . includes ( 'markdown' ) ) {
159
+ mimeTypes . push ( 'text/markdown' )
160
+ }
161
+ if ( allowedExtensions . includes ( 'epub' ) ) {
162
+ mimeTypes . push ( 'application/epub+zip' )
163
+ }
164
+ if ( allowedExtensions . includes ( 'eml' ) ) {
165
+ mimeTypes . push ( 'message/rfc822' )
166
+ }
167
+ if ( allowedExtensions . includes ( 'msg' ) ) {
168
+ mimeTypes . push ( 'application/vnd.ms-outlook' )
169
+ }
170
+
171
+ // 添加文件扩展名
172
+ const extensions = allowedExtensions . map ( ( ext ) => `.${ ext } ` )
173
+
174
+ return [ ...mimeTypes , ...extensions ] . join ( ',' )
175
+ }
176
+
119
177
/**
120
178
* 首字母大写
121
179
*/
0 commit comments