-
-
Save nkrth/95f4ad7b98e193478dcaf9978f32062f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var formidable = require('formidable'); | |
var fs = require('fs'); | |
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.filepath; | |
var oldname = files.filetoupload.originalFilename; | |
var ext = oldname.split('.')[1]; | |
var dt = new Date().toISOString().replace(/[-:.]/g, "_"); | |
var newname = oldname.split('.')[0] + '_' + dt + '_' + Math.floor(Math.random() * 10 ** 17) + '.' + ext; | |
console.log(newname); | |
var newpath = 'C:/Users/LENOVO/Desktop/New Folder/' + newname; | |
fs.rename(oldpath, newpath, function(err) { | |
if (err) throw err; | |
res.write('File uploaded and moved!'); | |
console.log("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); | |
console.log("Running") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment