新增:根据文件类型智能适配读取方式
This commit is contained in:
@@ -378,9 +378,89 @@ const readFile = async () => {
|
||||
}
|
||||
|
||||
addToHistory(filePath.value)
|
||||
|
||||
// 检查文件类型
|
||||
const ext = filePath.value.split('.').pop()?.toLowerCase() || ''
|
||||
|
||||
// 图片文件 - 提示无法预览
|
||||
const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp', 'ico', 'heic', 'heif']
|
||||
if (imageExts.includes(ext)) {
|
||||
Message.warning({
|
||||
content: '图片文件暂不支持预览,请使用图片查看器打开',
|
||||
duration: 3000
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 视频文件 - 提示无法预览
|
||||
const videoExts = ['mp4', 'avi', 'mkv', 'mov', 'wmv', 'flv', 'webm', 'm4v', 'rmvb', '3gp']
|
||||
if (videoExts.includes(ext)) {
|
||||
Message.warning({
|
||||
content: '视频文件暂不支持预览,请使用视频播放器打开',
|
||||
duration: 3000
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 音频文件 - 提示无法预览
|
||||
const audioExts = ['mp3', 'wav', 'flac', 'aac', 'ogg', 'wma', 'm4a', 'opus']
|
||||
if (audioExts.includes(ext)) {
|
||||
Message.warning({
|
||||
content: '音频文件暂不支持预览,请使用音频播放器打开',
|
||||
duration: 3000
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 可执行文件 - 警告
|
||||
const exeExts = ['exe', 'msi', 'app', 'dmg', 'deb', 'rpm', 'dll', 'so']
|
||||
if (exeExts.includes(ext)) {
|
||||
Modal.confirm({
|
||||
title: '⚠️ 警告',
|
||||
content: '这是一个可执行文件,读取可能显示乱码或包含二进制数据。是否继续?',
|
||||
onOk: async () => {
|
||||
await performFileRead()
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 压缩文件 - 提示
|
||||
const archiveExts = ['zip', 'rar', '7z', 'tar', 'gz', 'bz2', 'xz', 'cab', 'iso']
|
||||
if (archiveExts.includes(ext)) {
|
||||
Message.info({
|
||||
content: '压缩文件将以二进制方式显示,可能包含乱码',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
|
||||
// 其他文件直接读取
|
||||
await performFileRead()
|
||||
}
|
||||
|
||||
// 执行文件读取
|
||||
const performFileRead = async () => {
|
||||
fileLoading.value = true
|
||||
try {
|
||||
fileContent.value = await readFileApi(filePath.value)
|
||||
const content = await readFileApi(filePath.value)
|
||||
|
||||
// 检查文件大小和内容
|
||||
const ext = filePath.value.split('.').pop()?.toLowerCase() || ''
|
||||
const binaryExts = ['exe', 'dll', 'so', 'dylib', 'zip', 'rar', '7z', 'tar', 'gz', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']
|
||||
|
||||
if (binaryExts.includes(ext)) {
|
||||
// 二进制文件,限制显示大小
|
||||
if (content.length > 10000) {
|
||||
fileContent.value = content.substring(0, 10000) + '\n\n... (文件过大,已截断,仅显示前 10000 个字符) ...'
|
||||
Message.warning('文件较大,已截断显示')
|
||||
} else {
|
||||
fileContent.value = content
|
||||
}
|
||||
} else {
|
||||
// 文本文件,正常显示
|
||||
fileContent.value = content
|
||||
}
|
||||
|
||||
Message.success('文件读取成功')
|
||||
} catch (error) {
|
||||
console.error('读取文件失败:', error)
|
||||
|
||||
Reference in New Issue
Block a user