feat(上传组件): 添加SimpleFileUpload组件并集成到聊天窗口
- 创建新的SimpleFileUpload组件实现文件上传功能 - 在聊天窗口替换原有上传按钮为新的组件 - 为上传API添加调试日志 - 移除聊天窗口重复的音频图标导入
This commit is contained in:
97
Cunkebao/src/components/Upload/SimpleFileUpload/index.tsx
Normal file
97
Cunkebao/src/components/Upload/SimpleFileUpload/index.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import React, { useRef, useState } from "react";
|
||||
import { Button, message } from "antd";
|
||||
import { FolderOutlined, LoadingOutlined } from "@ant-design/icons";
|
||||
import { uploadFile } from "@/api/common";
|
||||
|
||||
interface SimpleFileUploadProps {
|
||||
onFileUploaded?: (filePath: string) => void;
|
||||
disabled?: boolean;
|
||||
className?: string;
|
||||
maxSize?: number; // 最大文件大小(MB)
|
||||
accept?: string; // 接受的文件类型
|
||||
}
|
||||
|
||||
const SimpleFileUpload: React.FC<SimpleFileUploadProps> = ({
|
||||
onFileUploaded,
|
||||
disabled = false,
|
||||
className,
|
||||
maxSize = 50,
|
||||
accept = "*/*",
|
||||
}) => {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
|
||||
// 验证文件
|
||||
const validateFile = (file: File): boolean => {
|
||||
if (file.size > maxSize * 1024 * 1024) {
|
||||
message.error(`文件大小不能超过 ${maxSize}MB`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// 处理文件选择
|
||||
const handleFileSelect = async (
|
||||
event: React.ChangeEvent<HTMLInputElement>,
|
||||
) => {
|
||||
const files = event.target.files;
|
||||
if (!files || files.length === 0) return;
|
||||
|
||||
const file = files[0];
|
||||
console.log("选择的文件:", file);
|
||||
console.log("文件名:", file.name);
|
||||
console.log("文件大小:", file.size);
|
||||
console.log("文件类型:", file.type);
|
||||
|
||||
if (!validateFile(file)) {
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = "";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
setUploading(true);
|
||||
|
||||
try {
|
||||
console.log("开始上传文件...");
|
||||
const fileUrl = await uploadFile(file);
|
||||
console.log("上传成功,文件URL:", fileUrl);
|
||||
onFileUploaded?.(fileUrl);
|
||||
message.success("文件上传成功");
|
||||
} catch (error: any) {
|
||||
console.error("文件上传失败:", error);
|
||||
message.error(error.message || "文件上传失败");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = "";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleClick = () => {
|
||||
if (disabled || uploading) return;
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept={accept}
|
||||
onChange={handleFileSelect}
|
||||
style={{ display: "none" }}
|
||||
/>
|
||||
<Button
|
||||
type="text"
|
||||
icon={uploading ? <LoadingOutlined /> : <FolderOutlined />}
|
||||
onClick={handleClick}
|
||||
disabled={disabled || uploading}
|
||||
className={className}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default SimpleFileUpload;
|
||||
Reference in New Issue
Block a user