You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

38 lines
1.4 KiB

var http = require("http");
var formidable = require("formidable");
var fs = require("fs");
var path = require("path");
http.createServer((req,res)=>{
if (req.url =="/fileupload"){
var form = new formidable.IncomingForm();
form.parse(req,function (err,fields,files){
var oldpath = files.fileToUpload.path;
var newpath = path.join(__dirname,"uploads",files.fileToUpload.name);
console.log(oldpath);
console.log(newpath);
fs.rename(oldpath,newpath,(err)=>{
if(err){
console.error(err);
res.writeHead(200,{"Content-type":"text/html"})
res.end("ISE");
return;
}
res.writeHead(200,{"Content-type":"text/html"})
res.write("File uploaded");
res.write('<a href="/">DOMU</a>')
res.end();
})
})
} else {
res.writeHead(200,{"Content-type":"text/html"});
res.write('<form action="fileupload" method="post" ');
res.write('enctype="multipart/form-data">');
res.write('<input type="file" name="fileToUpload"><br>');
res.write('<input type="submit">');
res.write('</form>');
res.end();
}
}).listen(8080)
console.log("Server bezi na http://localhost:8080")