阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame^] | 1 | // src/component/upload/upload.tsx |
| 2 | import React, { useState } from 'react' |
| 3 | import './upload.css' |
| 4 | |
| 5 | const UploadComponent: React.FC = () => { |
| 6 | const [selectedFile, setSelectedFile] = useState<File | null>(null) |
| 7 | |
| 8 | const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 9 | if (e.target.files && e.target.files.length > 0) { |
| 10 | setSelectedFile(e.target.files[0]) |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | const handleUpload = () => { |
| 15 | if (selectedFile) { |
| 16 | console.log('上传文件:', selectedFile) |
| 17 | // 此处可以添加实际上传逻辑 |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return ( |
| 22 | <div className="uploadContainer"> |
| 23 | <label className="uploadLabel" htmlFor="upload-input">请选择文件上传:</label> |
| 24 | <input |
| 25 | type="file" |
| 26 | id="upload-input" |
| 27 | className="uploadInput" |
| 28 | data-testid="upload-input" |
| 29 | onChange={handleFileChange} |
| 30 | /> |
| 31 | <label htmlFor="upload-input"> |
| 32 | <div |
| 33 | className="uploadButton" |
| 34 | data-testid="upload-button" |
| 35 | onClick={handleUpload} |
| 36 | > |
| 37 | 点击上传 |
| 38 | </div> |
| 39 | </label> |
| 40 | </div> |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | export default UploadComponent |