Skip to content

Commit 0bcde52

Browse files
committed
first commit
1 parent 1153fb9 commit 0bcde52

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

index.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!-- code by navnee1h -->
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
6+
<title>File Upload</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
background-color: #f4f4f4;
11+
margin: 0;
12+
padding: 0;
13+
display: flex;
14+
justify-content: center;
15+
align-items: center;
16+
height: 100vh;
17+
}
18+
19+
#upload-form {
20+
background-color: #fff;
21+
padding: 20px;
22+
border-radius: 8px;
23+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
24+
text-align: center;
25+
}
26+
27+
h2 {
28+
margin-top: 0;
29+
}
30+
31+
input[type="file"] {
32+
margin-bottom: 10px;
33+
}
34+
35+
input[type="submit"] {
36+
background-color: #4caf50;
37+
color: #fff;
38+
border: none;
39+
padding: 10px 20px;
40+
border-radius: 5px;
41+
cursor: pointer;
42+
transition: background-color 0.3s ease;
43+
}
44+
45+
input[type="submit"]:hover {
46+
background-color: #45a049;
47+
}
48+
</style>
49+
</head>
50+
<body>
51+
<div id="upload-form">
52+
<h2>Upload Multiple Files</h2>
53+
<form action="upload" method="post" enctype="multipart/form-data">
54+
<input type="file" name="files[]" multiple>
55+
<br>
56+
<input type="submit" value="Upload">
57+
</form>
58+
</div>
59+
</body>
60+
</html>
61+
<!-- code by navnee1h -->

server.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from http.server import BaseHTTPRequestHandler, HTTPServer
2+
import os
3+
import shutil
4+
import cgi
5+
6+
# Define the port for your server
7+
PORT = 8000
8+
9+
# Define the directory where uploaded files will be stored
10+
UPLOAD_DIR = "uploads"
11+
12+
class RequestHandler(BaseHTTPRequestHandler):
13+
def do_GET(self):
14+
if self.path == '/':
15+
self.send_response(200)
16+
self.send_header('Content-type', 'text/html')
17+
self.end_headers()
18+
with open("index.html", "rb") as f:
19+
self.wfile.write(f.read())
20+
else:
21+
self.send_error(404, "File not found")
22+
23+
def do_POST(self):
24+
form = cgi.FieldStorage(
25+
fp=self.rfile,
26+
headers=self.headers,
27+
environ={'REQUEST_METHOD':'POST'}
28+
)
29+
30+
# Create the uploads directory if it doesn't exist
31+
if not os.path.exists(UPLOAD_DIR):
32+
os.makedirs(UPLOAD_DIR)
33+
34+
# Save each uploaded file
35+
for field in form.keys():
36+
field_item = form[field]
37+
if isinstance(field_item, list):
38+
# Handle multiple files with the same field name
39+
for item in field_item:
40+
if item.filename:
41+
file_path = os.path.join(UPLOAD_DIR, os.path.basename(item.filename))
42+
with open(file_path, "wb") as f:
43+
# Write file data in chunks to handle large files
44+
shutil.copyfileobj(item.file, f, length=131072) # Adjust chunk size as needed
45+
else:
46+
# Handle single file upload
47+
if field_item.filename:
48+
file_path = os.path.join(UPLOAD_DIR, os.path.basename(field_item.filename))
49+
with open(file_path, "wb") as f:
50+
# Write file data in chunks to handle large files
51+
shutil.copyfileobj(field_item.file, f, length=131072) # Adjust chunk size as needed
52+
53+
# Send response
54+
self.send_response(200)
55+
self.end_headers()
56+
self.wfile.write(b"All files uploaded successfully")
57+
58+
def run():
59+
server_address = ('', PORT)
60+
httpd = HTTPServer(server_address, RequestHandler)
61+
print(f"Server running on port {PORT}")
62+
httpd.serve_forever()
63+
64+
if __name__ == '__main__':
65+
run()

0 commit comments

Comments
 (0)