Skip to content

Instantly share code, notes, and snippets.

@nkrth
Created November 24, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkrth/95f4ad7b98e193478dcaf9978f32062f to your computer and use it in GitHub Desktop.
Save nkrth/95f4ad7b98e193478dcaf9978f32062f to your computer and use it in GitHub Desktop.
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