Skip to content

Commit 54c9d4e

Browse files
feat: Support uploading files by copying, pasting, dragging and dropping (#2939)
1 parent d2637c3 commit 54c9d4e

File tree

1 file changed

+53
-0
lines changed
  • ui/src/components/ai-chat/component/chat-input-operate

1 file changed

+53
-0
lines changed

ui/src/components/ai-chat/component/chat-input-operate/index.vue

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
type="textarea"
138138
:maxlength="100000"
139139
@keydown.enter="sendChatHandle($event)"
140+
@paste="handlePaste"
141+
@drop="handleDrop"
140142
/>
141143

142144
<div class="operate flex align-center">
@@ -188,6 +190,7 @@
188190
:show-file-list="false"
189191
:accept="getAcceptList()"
190192
:on-change="(file: any, fileList: any) => uploadFile(file, fileList)"
193+
ref="upload"
191194
>
192195
<el-tooltip
193196
:disabled="mode === 'mobile'"
@@ -301,6 +304,8 @@ const localLoading = computed({
301304
}
302305
})
303306
307+
const upload = ref()
308+
304309
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
305310
const documentExtensions = ['pdf', 'docx', 'txt', 'xls', 'xlsx', 'md', 'html', 'csv']
306311
const videoExtensions = ['mp4', 'avi', 'mov', 'mkv', 'flv']
@@ -434,6 +439,54 @@ const uploadFile = async (file: any, fileList: any) => {
434439
}
435440
})
436441
}
442+
// 粘贴处理
443+
const handlePaste = (event: ClipboardEvent) => {
444+
if (!props.applicationDetails.file_upload_enable) return
445+
const clipboardData = event.clipboardData
446+
if (!clipboardData) return
447+
448+
// 获取剪贴板中的文件
449+
const files = clipboardData.files
450+
if (files.length === 0) return
451+
452+
// 转换 FileList 为数组并遍历处理
453+
Array.from(files).forEach((rawFile: File) => {
454+
// 创建符合 el-upload 要求的文件对象
455+
const elFile = {
456+
uid: Date.now(), // 生成唯一ID
457+
name: rawFile.name,
458+
size: rawFile.size,
459+
raw: rawFile, // 原始文件对象
460+
status: 'ready', // 文件状态
461+
percentage: 0 // 上传进度
462+
}
463+
464+
// 手动触发上传逻辑(模拟 on-change 事件)
465+
uploadFile(elFile, [elFile])
466+
})
467+
468+
// 阻止默认粘贴行为
469+
event.preventDefault()
470+
}
471+
// 新增拖拽处理
472+
const handleDrop = (event: DragEvent) => {
473+
if (!props.applicationDetails.file_upload_enable) return
474+
event.preventDefault()
475+
const files = event.dataTransfer?.files
476+
if (!files) return
477+
478+
Array.from(files).forEach((rawFile) => {
479+
const elFile = {
480+
uid: Date.now(),
481+
name: rawFile.name,
482+
size: rawFile.size,
483+
raw: rawFile,
484+
status: 'ready',
485+
percentage: 0
486+
}
487+
uploadFile(elFile, [elFile])
488+
})
489+
}
437490
// 语音录制任务id
438491
const intervalId = ref<any | null>(null)
439492
// 语音录制开始秒数

0 commit comments

Comments
 (0)