22301009 | df48f96 | 2025-06-05 13:40:44 +0800 | [diff] [blame^] | 1 | import React, { useRef, useState } from 'react'; |
| 2 | import axios from 'axios'; |
| 3 | |
| 4 | const SimpleUploader = () => { |
| 5 | const fileInputRef = useRef(null); |
| 6 | const [message, setMessage] = useState(''); |
| 7 | |
| 8 | const handleUpload = async () => { |
| 9 | const file = fileInputRef.current?.files[0]; |
| 10 | console.log('[handleUpload] file:', file); |
| 11 | |
| 12 | if (!file) { |
| 13 | setMessage('请先选择文件'); |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | const formData = new FormData(); |
| 18 | formData.append('file', file); |
| 19 | |
| 20 | try { |
| 21 | const response = await axios.post('/seeds/upload', formData, { |
| 22 | headers: { 'Content-Type': 'multipart/form-data' }, |
| 23 | }); |
| 24 | |
| 25 | console.log('[handleUpload] response:', response); |
| 26 | setMessage(response.data?.msg || '上传成功'); |
| 27 | } catch (err) { |
| 28 | console.error('[handleUpload] 上传失败:', err); |
| 29 | setMessage('上传失败'); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | return ( |
| 34 | <div style={{ padding: '2rem' }}> |
| 35 | <h2>种子上传测试</h2> |
| 36 | <input type="file" accept=".torrent" ref={fileInputRef} /> |
| 37 | <button onClick={handleUpload} style={{ marginLeft: '1rem' }}> |
| 38 | 上传 |
| 39 | </button> |
| 40 | <div style={{ marginTop: '1rem' }}>{message}</div> |
| 41 | </div> |
| 42 | ); |
| 43 | }; |
| 44 | |
| 45 | export default SimpleUploader; |