-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploader.js
More file actions
51 lines (42 loc) · 1.42 KB
/
FileUploader.js
File metadata and controls
51 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { useState } from 'react';
import axios from 'axios';
function FileUploader({ onTextReady }) {
const [file, setFile] = useState(null);
const [uploading, setUploading] = useState(false);
const [response, setResponse] = useState('');
const handleChange = (e) => {
const selected = e.target.files[0];
setFile(selected);
};
const handleUpload = async () => {
if (!file) return alert('파일을 선택해 주세요.');
const formData = new FormData();
formData.append('file', file);
try {
setUploading(true);
const res = await axios.post('http://localhost:5000/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
const extractedText = res.data.text;
setResponse(res.data.message);
onTextReady(extractedText); // 부모 컴포넌트로 텍스트 전달
} catch (err) {
setResponse('업로드 실패 😢');
} finally {
setUploading(false);
}
};
return (
<div style={{ padding: '20px', textAlign: 'center' }}>
<input type="file" accept=".txt,image/*" onChange={handleChange} />
<br /><br />
<button onClick={handleUpload} disabled={uploading}>
{uploading ? '처리 중...' : '업로드 및 문제 생성'}
</button>
{response && <p>{response}</p>}
</div>
);
}
export default FileUploader;