-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-fileupload-server.js
More file actions
65 lines (56 loc) · 1.9 KB
/
6-fileupload-server.js
File metadata and controls
65 lines (56 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createServer } from 'http';
import { formidable } from 'formidable';
import fs from 'fs';
import path from 'path';
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
if (req.url === '/fileupload' && req.method.toLowerCase() === 'post') {
const form = formidable({ multiples: false });
form.parse(req, (err, fields, files) => {
if (err) {
console.error('Formidable error:', err);
res.writeHead(500);
return res.end('File upload failed');
}
console.log('Parsed files:', files);
const uploadedFileArray = files.filetoupload;
const uploadedFile = Array.isArray(uploadedFileArray)
? uploadedFileArray[0]
: uploadedFileArray;
if (
!uploadedFile ||
!uploadedFile.filepath ||
!uploadedFile.originalFilename
) {
res.writeHead(400);
return res.end('Invalid file uploaded');
}
const oldPath = uploadedFile.filepath;
const uploadDir =
'C:/Users/Muhammad Zeeshan/Desktop/Berlin-tec/server-uploadfilesave/';
const newPath = path.join(uploadDir, uploadedFile.originalFilename);
fs.rename(oldPath, newPath, (err) => {
if (err) {
console.error('Rename error:', err);
res.writeHead(500);
return res.end('File move failed');
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('File uploaded and moved successfully!');
});
});
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(
'<form action="fileupload" method="post" enctype="multipart/form-data">'
);
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
res.end();
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});