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.
 
 
 

36 lines
1.3 KiB

//npm install formidable@1.2.2
var http = require("http");
var formidable = require("formidable");
var fs = require("fs");
var path = require("path");
http.createServer(function (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, function(err) {
if (err) {
console.error(err);
res.writeHead(200, {"Content-Type":"text/plain"});
res.end("ISE");
return;
}
res.write("File uploaded");
res.end();
})
})
} 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>');
return res.end()
}
}).listen(8080);