File size: 4,079 Bytes
ff72db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>파일 μ—…λ‘œλ“œ</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 50px auto;
            padding: 20px;
            text-align: center;
            border: 1px solid #ccc;
            border-radius: 8px;
            background-color: #ffffff;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        input[type="file"] {
            margin: 10px 0;
            padding: 10px;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .status {
            margin-top: 20px;
            text-align: left;
            font-size: 14px;
        }
        .status p {
            background-color: #e9f4ff;
            border-left: 4px solid #007bff;
            padding: 10px;
            margin: 5px 0;
        }
        nav {
            background-color: #333;
            padding: 10px;
            text-align: center;
        }
        nav ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }
        nav ul li {
            display: inline;
            margin: 0 15px;
        }
        nav ul li a {
            color: white;
            text-decoration: none;
            font-weight: bold;
        }
        nav ul li a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <header>
        <nav>
            <ul>
                <li><a href="/search">λ¬Έμ„œ 검색</a></li>
                <li><a href="/">파일 μ—…λ‘œλ“œ</a></li>
            </ul>
        </nav>
    </header>
    <div class="container">
        <h2>파일 μ—…λ‘œλ“œ</h2>
        <form id="upload-form" enctype="multipart/form-data">
            <input type="file" name="file" required>
            <button type="submit">μ—…λ‘œλ“œ</button>
        </form>
        <div class="status" id="status">
            <h3>μ—…λ‘œλ“œ μƒνƒœ</h3>
        </div>
    </div>

    <script>
        const form = document.getElementById('upload-form');
        const statusDiv = document.getElementById('status');
        const userId = "user123"; // 고유 μ‚¬μš©μž ID (λ™μ μœΌλ‘œ μ„€μ • κ°€λŠ₯)

        // WebSocket μ„€μ •
        const socket = new WebSocket(`ws://127.0.0.1:8000/ws/${userId}`);

        socket.onmessage = (event) => {
            const message = event.data;
            const p = document.createElement('p');
            p.textContent = message;
            statusDiv.appendChild(p);
        };

        socket.onerror = () => {
            const p = document.createElement('p');
            p.textContent = "WebSocket μ—°κ²° 였λ₯˜.";
            p.style.color = "red";
            statusDiv.appendChild(p);
        };

        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            statusDiv.innerHTML = '<h3>μ—…λ‘œλ“œ μƒνƒœ</h3>';
            const formData = new FormData(form);
            

            // 파일 μ—…λ‘œλ“œ μš”μ²­
            const response = await fetch(`/uploadfile/${userId}/`, {
                method: "POST",
                body: formData,
            });

            if (response.ok) {
                const p = document.createElement('p');
                p.textContent = "파일 μ—…λ‘œλ“œ 성곡!";
                statusDiv.appendChild(p);
            } else {
                const p = document.createElement('p');
                p.textContent = "파일 μ—…λ‘œλ“œ μ‹€νŒ¨.";
                p.style.color = "red";
                statusDiv.appendChild(p);
            }
        });
    </script>
</body>
</html>