feat: get file from clipboardData

This commit is contained in:
Lan
2025-03-02 13:27:36 +08:00
parent 8ba8cec7b1
commit 8e6c48e1b6
+18
View File
@@ -1,6 +1,7 @@
<template>
<div
class="min-h-screen flex items-center justify-center p-4 overflow-hidden transition-colors duration-300"
@paste.prevent="handlePaste"
>
<div
class="rounded-3xl shadow-2xl overflow-hidden border w-full max-w-md transition-colors duration-300"
@@ -456,6 +457,23 @@ const handleFileDrop = async (event: DragEvent) => {
}
}
const handlePaste = async (event: ClipboardEvent) => {
const items = event.clipboardData?.items
if (!items) return
for (const item of items) {
if (item.kind === 'file') {
const file = item.getAsFile()
if (file) {
selectedFile.value = file
fileHash.value = await calculateFileHash(file)
alertStore.showAlert('已从剪贴板添加文件:' + file.name, 'success')
break
}
}
}
}
const calculateFileHash = async (file: File): Promise<string> => {
return new Promise((resolve) => {
const chunkSize = 2097152 // 保持 2MB 的切片大小用于计算哈希