2 changed files with 38 additions and 4 deletions
@ -0,0 +1,38 @@ |
|||||
|
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") |
||||
Loading…
Reference in new issue