feat: 上传文件功能优化
- 只支持文本类型文件(txt、md、json、csv、代码文件等) - 最多读取 10000 字符,超出自动截取 - 文件内容附加到输入框,用户可编辑后发送 - 与用户消息一起提交给大模型处理 - 移除 PDF/DOC 等非文本文件支持
This commit is contained in:
75
www/app.js
75
www/app.js
@@ -3249,7 +3249,7 @@ function showAgentChatPage() {
|
||||
</div>
|
||||
</div>
|
||||
<input type="file" id="imageInput" accept="image/*" style="display:none">
|
||||
<input type="file" id="fileInput" accept=".txt,.md,.pdf,.doc,.docx,.json,.csv" style="display:none">
|
||||
<input type="file" id="fileInput" accept=".txt,.md,.json,.csv,.xml,.yaml,.yml,.log,.sql,.html,.css,.js,.py,.java,.c,.cpp,.go,.rs,.sh,.bash,.ini,.conf,.cfg" style="display:none">
|
||||
|
||||
<!-- 搜索栏 -->
|
||||
<div class="search-bar" id="searchBar">
|
||||
@@ -3780,7 +3780,7 @@ function openConversation(id) {
|
||||
</div>
|
||||
</div>
|
||||
<input type="file" id="imageInput" accept="image/*" style="display:none">
|
||||
<input type="file" id="fileInput" accept=".txt,.md,.pdf,.doc,.docx,.json,.csv" style="display:none">
|
||||
<input type="file" id="fileInput" accept=".txt,.md,.json,.csv,.xml,.yaml,.yml,.log,.sql,.html,.css,.js,.py,.java,.c,.cpp,.go,.rs,.sh,.bash,.ini,.conf,.cfg" style="display:none">
|
||||
</div>
|
||||
`;
|
||||
|
||||
@@ -5299,35 +5299,58 @@ async function handleFileUpload(e) {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = async (event) => {
|
||||
const content = event.target.result;
|
||||
const fileName = file.name;
|
||||
|
||||
// 添加用户消息
|
||||
currentConversation.messages.push({
|
||||
role: 'user',
|
||||
content: `[文件: ${fileName}]\\n\\n${content.slice(0, 500)}${content.length > 500 ? '...' : ''}`
|
||||
});
|
||||
|
||||
currentConversation.updatedAt = Date.now();
|
||||
saveConversations();
|
||||
renderMessages();
|
||||
|
||||
if (welcome) welcome.style.display = 'none';
|
||||
|
||||
// 调用AI生成
|
||||
await streamGenerateWithFile(content, fileName);
|
||||
};
|
||||
// 检查文件类型(只支持文本文件)
|
||||
const allowedExtensions = ['.txt', '.md', '.json', '.csv', '.xml', '.yaml', '.yml', '.log', '.sql', '.html', '.css', '.js', '.py', '.java', '.c', '.cpp', '.go', '.rs', '.sh', '.bash', '.ini', '.conf', '.cfg'];
|
||||
const fileName = file.name.toLowerCase();
|
||||
const isAllowed = allowedExtensions.some(ext => fileName.endsWith(ext));
|
||||
|
||||
// 根据文件类型读取
|
||||
if (file.name.endsWith('.pdf') || file.name.endsWith('.doc') || file.name.endsWith('.docx')) {
|
||||
// PDF/Word文件暂时只显示文件名
|
||||
showToast('PDF/Word文件暂不支持解析,请上传文本文件');
|
||||
if (!isAllowed) {
|
||||
showToast('只支持文本类型的文件(txt、md、json、代码文件等)');
|
||||
e.target.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查文件大小(限制约 10KB,大约 10000 字符)
|
||||
const maxSizeKB = 15; // 留一点余量
|
||||
if (file.size > maxSizeKB * 1024) {
|
||||
showToast(`文件太大,请上传小于 ${maxSizeKB}KB 的文本文件`);
|
||||
e.target.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
showToast('正在读取文件...');
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = async (event) => {
|
||||
let content = event.target.result;
|
||||
|
||||
// 限制最多 10000 字符
|
||||
if (content.length > 10000) {
|
||||
content = content.slice(0, 10000);
|
||||
showToast(`文件内容已截取前 10000 字符`);
|
||||
}
|
||||
|
||||
// 把文件内容附加到输入框,让用户可以添加额外说明
|
||||
const currentText = userInput.value.trim();
|
||||
const filePrefix = `【文件:${file.name}】\n内容如下:\n`;
|
||||
|
||||
if (currentText) {
|
||||
userInput.value = `${currentText}\n\n${filePrefix}${content}`;
|
||||
} else {
|
||||
userInput.value = `${filePrefix}${content}`;
|
||||
}
|
||||
|
||||
// 自动调整输入框高度
|
||||
autoResize(userInput);
|
||||
userInput.focus();
|
||||
|
||||
showToast('文件内容已添加到输入框');
|
||||
};
|
||||
|
||||
reader.onerror = () => {
|
||||
showToast('文件读取失败');
|
||||
};
|
||||
|
||||
reader.readAsText(file);
|
||||
e.target.value = '';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user